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

useEffect does not trigger a rerender on the primary parent component

I am facing an issue where the main parent component does not re-render when I change the state 'click button' in another component while using useEffect. Oddly enough, the main <App /> component only re-renders properly when I reload the p ...

Can someone provide guidance on utilizing the index correctly within this v-for to prevent any potential errors?

I am encountering an issue with using index in a v-for loop to implement a function that deletes items from an array. The linter is flagging "index is defined but never used" as an error. I am following the instructions provided in a tutorial, but I am un ...

Can you guide me on setting a background image URL for rails using javascript?

Within my Rails application, I have a collection of content paired with image buttons (each piece of content has an associated image). When a user clicks on one of these buttons, my goal is to display the corresponding image. All of my images are stored u ...

Highcharts - Troubleshooting the chart reflow feature

Take a look at the fiddle I created. I encountered an issue with the chart width when toggling the sidebar. After seeking help on Stack Overflow from this post, I was able to solve it. Now, I'm facing another bug where adding transitions while togg ...

Using Angular 6 to import GeoJSON into a Leaflet map

I am facing an issue while trying to import a GeoJson file into Leaflet in my Angular app version 6. Although the geojson is being successfully drawn on the leafletmap, I am encountering an error that is preventing me from building my app. Is there anyone ...

Choose a random cell in Jquery every second

I am searching for a method to choose one div out of sixteen and change its CSS backgrounds. This selection needs to occur every second, so a new div is selected from the group each second. I am struggling with implementing the "every second" functionalit ...

Floating action button within a collapsible panel

I am having trouble placing a fixed-action-btn inside a collapsible body. It keeps appearing in the bottom right corner of the page instead of within the collapsible itself. How can I ensure that the button stays inside the body? Check out this link for r ...

Finding the Middle Point of a Circle Using CEP/JavaScript

Using a specific set of calculations, I am able to create a circle in this manner: var yMinCir = height - 1515.80/2.667+8.5; var yMaxCir = height - 1545.80/2.667+8.5; var myCircle = page.ovals.add({geometricBounds:[yMinCir, 1312.63/2.667+8.5, yMaxCir, 1342 ...

Separate mathematical expressions into an array without breaking apart any subexpressions inside parentheses or single quotes

Consider this given string: 1 + 2 * (3 + (23 + 53 - (132 / 5) + 5) - 1) + 2 / 'test + string' - 52 The task is to divide the string into an array of operators and non-operators, while preserving the content between () and '. The desired ou ...

The formControl value is not being shown on the display

I am facing an issue with a form that has multiple fields created using formGroup and formControlName. The challenge arises when I launch the application and need to retrieve data from the back end to display it on the view. The specific problem I'm ...

Setting up craco for jsx in your project

I am currently utilizing craco and grappling with how to configure jsx. I keep encountering the error below: Support for the experimental syntax 'jsx' isn't currently enabled (4:17): The suggestion is to add `babel/preset-react or utilize ...

Ways to transmit information among Vue components

I am trying to figure out how to pass data from the Module variable in CoreMods.vue to ExternalWebpage.vue using Vuex. I want the CoreModule state to be watched for changes in the external webpage. This is my store.js setup: import Vue from 'vue ...

Guide on inserting a dropdown menu following a specific count of <li> elements using Javascript or Jquery

I am currently working on an HTML project <div class="ktmsg"> <ul> <li class="a1"> <a title="Link Tools" href="#"> … </a> </li> <li class="a2"> <a title="Link Tools" href="#"> ...

Creating a table in VueJs and populating it with values retrieved from an MSSQL database in a .NET Core web application

I am developing a table within a .NET Core Web Application that includes multiple rows and columns filled with data retrieved from a MSSQL server through a WEB API Given the need for numerous rows and columns, I am considering using a for loop inside my & ...

Technique for seamlessly moving between subpages and main page while scrolling to a specific id

I'm struggling with coding and thought I'd reach out for help here. Can anyone provide a solution to my problem? Issue - I have a navigation link on my main page that scrolls to an ID using jQuery. It works fine on the main page, but not on any ...

Tips on toggling the visibility of div elements with JavaScript

In my selection block, I have three categories of elements and associated Divs. The goal is to display the related divs when a category is selected, while keeping them hidden otherwise. Specifically, I want the husband_name and number_pregnancy divs to be ...

Exploring ES6: Harnessing the Power of Classes

I am currently learning the ES6 syntax for classes. My background is in C#, so I apologize if my terminology is not accurate or if something seems off. For practice, I am working on building a web app using Node and Express. I have defined some routes as ...

Investigating TLS client connections with node.js for troubleshooting purposes

I am currently facing an issue while setting up a client connection to a server using node.js and TLS. My query revolves around how I can gather more information regarding the reason behind the connection failure. It would be great if there is a way to ob ...

Having trouble choosing the component-button using Protractor

I'm having trouble selecting the "Add New" button using any locator component. Check out the audience.po.ts file and the method "ClickAddNewBtn()": clickAddNewBtn() { console.log("Clicking on the Add New button."); return element(by.cs ...

Click to add a card

Attempting to incorporate a new row with the click of a button dynamically has proven to be challenging for me. As someone who is relatively new to JavaScript, I am struggling to figure it out. In the demonstration provided below, you will notice that noth ...