Is there a way to efficiently retrieve multiple values from an array and update data in a specific column using matching IDs?

In my Event Scheduler spreadsheet, I am looking for a way to efficiently manage adding or removing employees from the query table in column A. Currently, I have a dropdown list in each row to select names and a script that can only replace one name at a time on the database sheet upon clicking a button. However, this method is not practical when needing to update multiple names simultaneously. Is there a way to modify the function so it can replace multiple names at once instead of just one? Any assistance would be greatly appreciated. Below is the code snippet I currently have for replacing a single value.

function saveRecord7() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const formWS = ss.getSheetByName('Dashboard')
  const dataWS = ss.getSheetByName('Static_VDatabase_UID')
  const idCell = formWS.getRange('G7')
  const fieldRange =["AB7"]
  const clearcell = formWS.getRange('AB7')

  const id = idCell.getValue()
  if(id== ''){
    return
  }
  
   const cellFound = dataWS.getRange("A:A")
                  .createTextFinder(id)
                  .matchCase(true)
                  .matchEntireCell(true)
                  .findNext()

    if(!cellFound) return  
    const row = cellFound.getRow()            
  
   const fieldValues = fieldRange.map(f => formWS.getRange(f).getValue())
   fieldValues.unshift()
   dataWS.getRange(row,20,1,fieldValues.length).setValues([fieldValues])  

 clearcell.clearContent();
}

Answer №1

function updateRecords() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const formSheet = spreadsheet.getSheetByName('Form');
  const dataSheet = spreadsheet.getSheetByName('Data');

  // Defining the ranges for IDs and values
  const idRange = formSheet.getRange('A1:A'); // Adjust the range as needed
  const valueRange = formSheet.getRange('B1:B'); // Adjust the range as needed

  const ids = idRange.getValues().filter(String); // Remove empty rows
  const values = valueRange.getValues().filter(String); // Remove empty rows

  if (ids.length !== values.length) {
    throw new Error('Mismatch between IDs and values length');
  }

  // Update records based on ID
  ids.forEach((idArray, index) => {
    const id = idArray[0];
    if (id === '') {
      return; // Skip empty IDs
    }

    const cellFound = dataSheet.getRange("A:A")
      .createTextFinder(id)
      .matchCase(true)
      .matchEntireCell(true)
      .findNext();

    if (!cellFound) return;
    const row = cellFound.getRow();

    // Update the value in a specific column
    dataSheet.getRange(row, 5).setValue(values[index][0]);
  });

  // Clear input ranges after updating records
  idRange.clearContent();
  valueRange.clearContent();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing $.getJSON to initiate a selection change event

I'm currently working on implementing a feature that involves adding categories to a dropdown list using jQuery Ajax. The goal is to load subcategories when a particular option is selected. However, I've encountered an issue where the addition o ...

Ajax fails to provide a response

Hey there, I'm trying to save the response from an HTML page and store the content of that page. However, every time I try, I keep getting an error message. What am I doing incorrectly? <script type="text/jscript"> var page = ""; $.aj ...

Basic JavaScript string calculator

I'm in the process of creating a basic JavaScript calculator that prompts the user to input their name and then displays a number based on the input. Each letter in the string will correspond to a value, such as a=1 and b=2. For example, if the user e ...

Binding arguments to child functions within Vue components

When working with React, it's a common practice to bind parameters for child components in the following manner: <Child onChange={e => doThing(complex.variable.inParentScope[3], e.target.value)} foo="bar" /> In Vue, I want to ach ...

Creating a Dynamic Tree View Component in AngularJS Using JSON Data

I am new to AngularJS and I need help creating a TreeView Structure from a JSON Object. Here is an example of my Return JSON Object: var categoryTree = [{Name:'Item1', Childnodes : {}, id: 1}, {Name:'Item2', Childnod ...

An unexpected type error occurred: Unable to read the undefined property 'map' when utilizing Highcharts

I am currently working on developing highcharts using data from Firebase. I came across a helpful example here: However, when I try to integrate it into my application, I encounter the following error: firebase.js:43 Uncaught TypeError: Cannot read pr ...

Obtain form data as an object in Vue when passed in as a slot

Currently, I am working on developing a wizard tool that allows users to create their own wizards in the following format: <wiz> <form> <page> <label /> <input /> </page> <page> <label /> ...

Creating a Delicious Earth Progress Pie

I'm looking to incorporate a unique progress bar design on my website, similar to this one: The progress pie Can anyone guide me on how to create a progress pie with an image inside it that changes color as it moves? ...

Encountering a "Raphael is undefined" error message when working with Treant.js

I need help creating an organizational flow chart using treant.js. Below is my code snippet, but I'm encountering a 'Raphael is not defined' error that I can't seem to solve. Can someone please assist me with identifying the root cause ...

Unable to modify page property status through the Notion API

I've been attempting to use the Notion JS-sdk to update a page's status using their API. However, I've run into some issues that I can't seem to resolve. Updating the status requires modifying the properties object, but no matter what ...

Generate an array resembling a list when displayed in PHP

My objective is to display my database results in a format that resembles an array, even though it doesn't have to be an actual array. For example, when I echo my result, I wish for the final output to resemble [10,200,235,390,290,250,250] However ...

The ajaxStart() and ajaxStop() methods are not being triggered

I'm currently working on a Q/A platform where users can click on specific questions to be redirected to a page dedicated for answers. However, when a user tries to answer a question by clicking the "Answer" link, certain background processes such as ...

Automatically filling in related information in multiple input fields after choosing a value in a specific input field using JavaScript

My horizontal form is dynamically generated using JavaScript, with each input field having a jQuery autocomplete that retrieves data from a database using the $.get method. I'm looking to automatically populate the second input field with the corresp ...

loading JSON file using dynamic JavaScript techniques

My Raspberry Pi is running a C++ application in the background that performs complex mathematical calculations based on sensor input and generates results every second. I want to showcase this data on a website by having the application create a JSON file ...

JavaScript: Creating a Function that Returns an Object with an Undefined `this.variable`

While using javascript, I encountered an issue with instance variables. Declaring a variable as "this.variable" works fine until my function returns an object. However, if the function returns a String or Number, there are no issues. But when it returns ...

Blending Angular5 and AngularJS in Polymer

We are considering launching two new projects - one using Angular 5 and the other utilizing Polymer. The second project is intended to serve as a component library for reuse in not only the Angular 5 project but also in other AngularJS projects. After res ...

Update the package.json file by adding a new command to an existing script

Is it possible to automatically run npm install before starting the application with npm start? Here is what my package.json file currently looks like: . . "scripts": { "test": "echo \"Error: no test specified\ ...

Retrieve a div element using two specific data attributes, while excluding certain other data attributes

Here are some examples of divs: <div id="1" data-effect-in="swing" data-effect-out="bounce"></div> <div id="2" data-effect-in="swing"></div> <div id="3" data-effect-out="swing"></div> <div id="4" data-effect-out data ...

Live Update Google Sheet Data to JSON Without Reloading Web Page

This particular function is executing smoothly. My main concern lies in updating a DOM element without reloading the webpage, if any alterations are made to the data on a Google sheet I am utilizing a JSON file from Google sheets: https://spreadsheets.g ...

Traversing through nested arrays within nested objects using AngularJS

In my project, I am utilizing angularjs 1 and dealing with a complex json object that has multiple levels of nesting. My goal is to use ng-repeat in order to access a nested array within the json structure. [{ "information": { "name": "simdi jinki ...