Guide on updating individual rows in Google App Script using data from a different sheet

I am trying to create a script that will pull a value from column[3] in the ZONE sheet to the active sheet, specifically in column 56 of the job sheet when the zonelist value matches the zone value in different sheets. The script should check the range from row 2 to the lastRow in the Job sheet and set the corresponding value from the zone sheet to each respective cell in the job sheet. However, when I run the script, it is displaying the same value in all rows of column 56 in the job sheet instead of transferring the appropriate values based on matching zones.

function UpdateZoneRate(){
   
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('JOBS'); 
   
  var jobSheet = ss.getSheetByName('JOBS');
  var zoneSheet = SpreadsheetApp.openById(
    "1jbG_PLWU_eXfQTkwUn1krtYfzniw7HwnwnunhohzL61").getSheetByName('ZONE'); 
  var lastzone = zoneSheet.getLastRow();
  var zoneList = zoneSheet.getRange(2,1,lastzone,4).getValues();
  var newProd = jobSheet.getLastRow();
  var productCode = jobSheet.getRange(2,16,newProd,1).getValue();
  var newZone = jobSheet.getLastRow();
  var zone = jobSheet.getRange(2,53,newZone,1).getValue();
 
  for (j = 0;j <zoneList.length;j++){
      if (zoneList[j][0] == zone && productCode.match(/^A/i))
        var aggZone = zoneList[j][3];
        jobSheet.getRange(2,56,newZone,1).setValue(aggZone)}
                             
   }

SAMPLE VALUES FROM ZONESHEET ZONE1 2 ZONE2 4 ZONE3 6

CURRENT OUTPUT FROM SCRIPT ZONE1 2 ZONE2 2 ZONE2 2

CORRECT OUTPUT REQUIRED FROM THE SCRIPT ZONE1 2 ZONE2 4 ZONE2 4

Thank you for any assistance

Answer №1

Per your previous response,

The objective is to extract data from column "D". Upon script execution, it will cross-reference the cell values in column BA of the JOBS sheet with those in column A of the ZONE sheet, and then transfer the corresponding value from column D to column BD on the JOB sheet.

Does the anticipated script align with the following?

Example script:

If so, please copy and insert the subsequent code into the spreadsheet's script editor, encompassing the "JOBS" sheet.

function example() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var jobSheet = ss.getSheetByName('JOBS');
  var zoneSheet = SpreadsheetApp.openById("1jbG_PLWU_eXfQTkwUn1krtYfzniw7HwnwnunhohzL61").getSheetByName('ZONE');
  var obj = zoneSheet.getRange("A2:D" + zoneSheet.getLastRow()).getValues().reduce((o, r) => (o[r[0]] = r[3], o), {});
  var range = jobSheet.getRange("BA2:BD" + jobSheet.getLastRow());
  var values = range.getValues().map(r => [obj[r[0]] || null]);
  range.offset(0, 3, values.length, 1).setValues(values);
}
  • Upon running this script, data is fetched from the "JOBS" sheet within the current spreadsheet as well as the "ZONE" sheet from another. It compares column "A" of "JOBS" with column "BA" of "ZONE", ultimately transferring data from column "D" in "JOBS" to column "BD" in "ZONE".

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

Text that is curving around a D3.js pie chart

I am currently working on creating a 3D-Js chart and I would like the pie text to wrap around the pie itself. This is the exact effect that I am trying to achieve: I am facing two main issues: I am currently printing two separate charts (with labels prin ...

Is an audio player/playlist necessary for showcasing a mix engineer's skills in their

As a newcomer to the world of web development with some background knowledge from school, I work as a mix engineer and have created a portfolio website. Previously, I utilized Soundcloud and Spotify's API to showcase my mixes/songs, but the external J ...

Managing key presses with functions in VueJs

Within my component, I am utilizing VueStrap's modal in the following manner: <template> <modal-window v-model="show" v-on:keyup="keyHandler($event)" @ok="submit()" @cancel="cancel()" @closed=" ...

Using Node.js to schedule and send notifications to a specific topic via FCM

In order to deliver topic notifications to Android devices using FCM, I have set up an Angular application and a Node.js server. The scheduling of these notifications is handled by the bull library. The process involves two methods of sending notification ...

The method item.appendChild does not exist as a function

Despite being a common error, I've researched extensively and still can't figure out why it's happening. It seems like it should be an easy fix, but I'm struggling to find the solution on my own. var item = document.createElement("div" ...

React error: File undefined (cannot be exported)

Encountering an issue while attempting to follow a tutorial: src/components/MyApp.js Line 17:18: 'myApp' is not defined no-undef Search for the keywords to learn more about each error. This is what my myApp.js file contains: import React fr ...

Sharing golang gin session with next.js

Utilizing the latest version of Next.js v14.2.3 and App Router. I am currently implementing cookie-based sessions from the gin-contrib documentation, in order to increase a session count. // Backend Golang code snippet ... cookieStore := sessi ...

What is the most effective method for integrating additional CSS/JS libraries into a GitHub repository?

I would like to integrate FontAwesome, Bulma, jquery, and jquery-ui into one of my Github repositories for the front-end section. Currently, I am including the JS files or CSS files from these projects in my own JS/CSS folders, but I believe there might ...

Changing the i18n locale in the URL and navigating through nested routes

Seeking assistance to navigate through the complexities of Vue Router configurations! I've dedicated several days to integrating various resources, but have yet to achieve successful internalization implementation with URL routes in my unique setup. ...

"An issue with AngularJS ngTable causing a delay in the rendering of Ajax

Struggling with ngTable Setup Having trouble mastering the use of ngTable by going through the ngTable ajax demo. I attempted to follow along with the example as closely as possible, but I'm encountering a deviation in defining an ngResource inline w ...

The custom div slider speed is too quick, specifically when transitioning back from a different browser tab

While observing the code in action, everything seems to work fine but when I switch to another browser tab and come back, the speed of the animation is too fast. Below is the jQuery code snippet: var divId = 1; var lp = 0; $(document).ready(function (){ ...

The use of jQuery ajax requests is leading to a refresh of the page

I've encountered an issue with a button on my HTML page that is not associated with any form. <input type='button' id='submitter' value='add'/> There is a click handler attached to it: $('#submitter').c ...

'Error: Object does not have access to the specified property or method 'values'

I've been working on writing some code to retrieve and read a JSON file. It seems to work fine in Chrome, but I'm running into issues with IE11, which is the browser I need to use. I've tried changing variable names, but the problem persists ...

How to Handle an Empty Route with a Trailing Slash in Backbone Routing?

While I am aware of the potential SEO issues that come with duplicate content, my project is not currently focused on that aspect. Looking at my backbone router configuration, here it is: routes: { "": "startOrder", "order/:orderNumber/:stepName" ...

Generating a collection of items within a JavaScript object

Struggling with sending a json object to a java API backend that requires an Object containing a list of objects. Wondering if it's possible to create a list of objects inside a javascript Object. While I know we can create an "Array" of objects with ...

Using AngularJS to filter JSON data

Greetings! I possess the following JSON data: $scope.Facilities= [ { Name: "-Select-", Value: 0, RegionNumber: 0 }, { Name: "Facility1", Value: 1, RegionNumber: 1 }, { Name: ...

How can one utilize electron's webContents.print() method to print an HTML or text file?

Electron Version : 2.0.7 Operating System : Ubuntu 16.04 Node Version : 8.11.1 electron.js let win = new BrowserWindow({width: 302, height: 793,show:false}); win.once('ready-to-show', () => win.hide()); fs.writeFile(path.join(__dirname ...

Proper utilization of ngIf in conjunction with mat-cell

I am attempting to show a specific value only if the item possesses a certain property, but I keep seeing [object Object] instead. Here is my current method: <ng-container matColumnDef="name"> <th mat-header-cell *matHeaderCellDe ...

Modifying the .textcontent attribute to showcase an image using JavaScript

I am working on a website and I want to change editButton.textContent = 'Edit'; so that it displays an image instead of text. var editButton = document.createElement('button'); editButton.textContent = 'Edit'; After exploring ...

The NextAuth file imported with the EmailProvider is currently not being utilized

Having an issue with implementing nextauth for authentication. I have imported EmailProvider from nextauth/providers but when I try to use it in the Nextauth providers object, it does not recognize the EmailProvider that I've imported. Here is the co ...