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

The Material UI dialog box popped up against an unexpected gray backdrop

Currently, I am using Material UI in conjunction with React to create a dialog that appears when a button is tapped. This button is located within a table, which is displayed over a Paper component. The problem arises when I utilize the dialog with its def ...

Communication between a directive controller and a service via an HTTP call

I'm currently developing an Angular directive that loads a Highchart.js area graph by passing some variables to it. Here's how I am using the directive: <andamento-fondo-area-chart color="#3FAE2A" url="../data.json"></andamento-fondo-a ...

Using a Do/While loop in combination with the setTimeout function to create an infinite loop

Having trouble with this script. The opacity transition works fine, but there seems to be an issue with the loop causing it to run indefinitely. My goal is to change the z-index once the opacity reaches 0. HTML <div class="ribbon_services_body" id="ri ...

Boost your website's loading time by utilizing the async and defer attributes

Looking to enhance the speed of my webpage, particularly focusing on improving initial page speed. Research suggests that using the async and defer attributes for JavaScript can be beneficial. All JavaScript scripts are currently placed just above the cl ...

Ember JS: Master of Controlling

I am working with the following controllers: clusters_controller.js.coffee Portal.DashboardClustersController = Ember.ArrayController.extend dashboard_controller.js.coffee Portal.DashboardController = Ember.ArrayController.extend In my template, I am ...

Executing an Ajax request to a document containing <script> elements

Recently, I developed a sample page that effectively mimics table layout pages but without relying on the traditional mobile-unfriendly table features. The structure of the page is as follows: <html> ... <body> <div type="page" ...

What is the best approach for establishing an asynchronous connection to a MongoDB database?

Managing a MongoDB database using JavaScript and Node.js with Mongoose, I needed to retrieve an array containing the names of all collections in the database. Taking this into consideration, I implemented the following code snippet. let connection = mongoo ...

What is causing the error message "Error: Cannot update active font: 'Fira Sans' is not included in the font list" in react-font-picker?

For my project, I have implemented a font picker component in the following manner: <FontPicker apiKey={process.env.REACT_APP_GOOGLE_API_KEY} activeFontFamily={activeFontFamilyMobile} ...

Updating the model of a Vuejs single file component (.vue) within the <script> tag

Discovering the world of vuejs, I set out to create a basic single file component for testing purposes. This component's main task is to showcase a boolean and a button that toggles the boolean value. It also listens for a "customEvent" which trigger ...

Merge JavaScript Functions into a Single Function

I am looking to streamline the following javascript code into a single function by utilizing an array of ids instead of repetitive blocks. Any suggestions on how to achieve this would be greatly appreciated. Currently, in my code, I find myself copying an ...

Easily load events into a responsive calendar widget with w3widgets. No

When attempting to load events dynamically, I encountered an issue where the output was not displaying correctly. I used AJAX to retrieve data in the following format: var datalist = "2015-09-22":{} $(".responsive-calendar").responsiveCalendar({ eve ...

Utilizing Vue.js to incorporate the isActive property to the class name within a v-for loop, along with implementing dynamic class names

I am currently using a loop to iterate through some data in my store.js file, and everything is functioning as expected. However, I would like to add a condition to check if the Vue instance is active before applying a specific class to the polygon element ...

Setting a CSS Variable with the Help of jQuery

I need help with changing the background color of a specific .div element when it is clicked on. I want the color to be a variable that can be changed by the user, and this change should occur when the document is ready. Currently, I am using a CSS variab ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Setting the default tab in easyTabs to be all-powerful

My ajax content is being loaded into a div-container through various navigation links, and I am using the script below to establish the defaultTab: $(document).ready( function() { $("#tab-container").easytabs({updateHash: true, defaultTab: "li:eq(3)"}); ...

When implementing dynamic routing in Next.js, an error occurs with TypeError: the 'id' parameter must be a string type. It is currently

I’m encountering a problem while creating dynamic pages in Next.js. I'm fetching data from Sanity and I believe my code is correct, but every time I attempt to load the page, I receive a type error - “the ‘id’ argument must be of type string. ...

Halting execution: Trying to use the keyword 'import' which is not allowed here

0. April 2023: error cannot be reproduced anymore see here The error is no longer replicable due to bug fixes in react-scripts 5.0.1. 1 Even though the error is gone, the question and my self-answer still seem relevant to Angular users and others as we ...

What is the correct way to update the state of an object in ReactJS using Redux?

Hello, I am facing an issue with storing input field values in the state object named 'userInfo'. Here is what my code looks like: <TextField onChange={this.handleUserUsername.bind(this)} value={this.props.userInfo.username} /> ...

View cards from a restricted Trello board without requiring visitors to have a Trello account or to authorize through a popup

I have a company with ongoing projects listed on a private Trello board. We are interested in showcasing these projects on our website in real-time by connecting to the board. After implementing this example, I can successfully retrieve and display the ca ...

Interactive feature on Google Maps information window allowing navigation to page's functions

Working on an Angular2 / Ionic 2 mobile App, I am utilizing the google maps JS API to display markers on a map. Upon clicking a marker, an information window pops up containing text and a button that triggers a function when clicked. If this function simpl ...