Contrast 4 distinct columns across 2 separate Google Sheets

In an attempt to compare data from two Google Sheets, I am facing a challenge. Each sheet contains a column that serves as the identifier (Sheet1:H and Sheet2:C). My goal is to update the value in Sheet1:I to match the value in Sheet2:E when the identifiers match. Despite running the provided code without any errors, it does not seem to be working as intended.

While looking for solutions online, I noticed that most posts did not address the specific method of comparing different columns that I am using.

function changestatus() {
  // retrieve spreadsheet A and data range
  ssA = SpreadsheetApp.openById('IDHERE');
  sheetA = ssA.getSheetByName('Sheet1');
  dataA = sheetA.getRange('H2:H').getValues();
  dataD = sheetA.getRange('I2:I').getValues();

  // retrieve spreadsheet B and data range
  ssB = SpreadsheetApp.openById('IDHERE');
  sheetB = ssB.getSheetByName('responses');
  dataB = sheetB.getRange('C2:C').getValues();
  dataC = sheetB.getRange('E2:E').getValues();

  for (var i = 0; i > sheetA.getLastRow(); i++) {
    if (dataA[1][i] == dataB[1][i] && dataC[1][i] != dataD[1][i]){
      var value = sheetA.getRange(i+1, 2).getValue(dataD);
      sheetB.getRange(i+1, 2).setValue(value);
    } // end if
  } // end i

The initial results from both sheets would look like this:

Sheet 1
H:(ID) 1 I:(grade) pass

Sheet 2
C:(ID) 1 E:(grade) fail

After running the function:

Sheet 1
H:(ID) 1 I:(grade) fail

Answer №1

@tehhowch pointed out some crucial areas for improvement:
To resolve the code problems, you should focus on revisiting JavaScript comparison operators, adjusting for loop syntax, understanding the format of objects returned by Range#getValues, and mastering how to access array indices in JavaScript.

For Loop Syntax Correction
Ensure that in your loop syntax, instead of "i > sheetA.getLastRow()", it should be "i < sheetA.getLastRow()". This adjustment allows the loop to process all values of 'i' less than the last row's value.

Array Values Clarification
Since getValues returns a two-dimensional array, the array values may not align as expected. For instance, change from "dataA[1][i]" to dataA[i][0] with an explanation provided for each alteration.

Troubleshooting Tip
In cases where the IF statement seems correct but still poses issues, consider using Logger.log() to display or log the returned values for analysis within the script editor under "View, Logs".

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

Obtain the three-byte counter from an ObjectId in Mongodb

Is there a way to extract the 3-byte counter, which starts with a random value, from a MongoDB ObjectId? I currently have an ObjectId that looks like this: ObjectId("507f1f77bcf86cd799439011") From what I understand in the MongoDB documentation: Descript ...

Finding the size of an integer array in Java

Here is the code that is causing an error: Cannot invoke size() on the array type int[] Code: public class Example{ int[] array={1,99,10000,84849,111,212,314,21,442,455,244,554,22,22,211}; public void Printrange(){ for (int i=0;i<array.size();i+ ...

Tips for showing a popup alert depending on a specific value

My dropdown menu includes the options nothing, name1, name2, name3, name4 and more,.... I want an alert popup to display only when I select nothing, but not when I choose any of the other options. Any suggestions on how to achieve this? ...

Calculating the total sum of values from various ajax requests

My project involves a set of dropdowns containing player names. Upon selecting a player, their 'Value' should be displayed next to the dropdown, and the Total field should update to show the sum of all selected players' values. The PHP code ...

Adding information into material-ui dropdown using React JS

I could use some assistance with populating data into a Dropdown using material-ui in React. I am new to React and unsure about how to achieve this. I know that I can pass props to the dropdown, but it's not very clear to me. Here is my current code: ...

Angular asynchronous request does not yield any results

After executing ngOnChanges, the method _generateLocationFormForApproval is called, resulting in the output this.absoluteUri. Following this, _pageEventDealsForApprovalList is invoked. However, when trying to access the value of this.absoluteUri inside _pa ...

Creating React elements dynamically with material-ui can be done by utilizing state expressions in properties. This allows for the generation

Within a functional component, I aim to dynamically generate material-ui context menus by utilizing a state object: let legendContextMenuStatesObject = {}; for (let key of keys) { legendContextMenuStatesObject[key] = initialState; } const [lege ...

Jquery outputs JSON data in a string format

When transferring a JSON object from PHP to JQuery Ajax, I am encountering an issue where the data is received as a string rather than an object. In my PHP code: $userdata=json_encode(array("FirstName"=> $fName,"LastName"=>$lName,"PhoneNumber"=> ...

Puzzle about initializing an array with a repetitive value as a sequence in Swift

Exploring ways to pass a dynamically generated sequence, such as generating key strings while initializing an array. Consider this array initialization code snippet: let MyArray: Array<Int> = Array<Int>(count: 100, repeatedValue:(for i in 0.. ...

What is preventing the addition of links to the navigation bar when using a sticky navigation bar?

Currently, I am in the process of developing a blog website using Django. One of the features I have successfully implemented is a sticky navigation bar. However, I am facing a challenge with adding links to the menu on the navigation bar due to existing ...

Is it possible to utilize WebRTC (simple-peer) with STUN without the need for additional signaling?

I am currently exploring the utilization of the simple-peer library to create browser-to-browser WebRTC connections through data channels. My understanding, although it may be flawed, is that for two browsers to establish a connection via WebRTC, they need ...

FullCalendar, showcasing real-time event creation as you select dates

I am currently utilizing fullcalendar 4 on my website and I am attempting to incorporate an indicator for when a user selects a date on the calendar. While the background color changes, it's not very visible. My aim is to replicate the functionality ...

The endpoint 'pusher/auth' returned a 404 error code indicating that it was

I'm currently setting up a private channel using Pusher on a local node.js server. Strangely, I'm encountering a 404 error with my auth endpoint. Initially, I suspected a problem with how I defined the endpoint in relation to the local server&ap ...

Using a numeral within a Font Awesome icon symbol to customize a label for a Google Maps marker

I have a Google Maps marker displayed below, applying a Font Awesome icon as the label/icon. However, I am unsure how to a.) change the color of the marker and b.) include a number inside the marker. Referencing this Stack Overflow post This is the code ...

Unable to retrieve POST information

Currently delving into backend node/express. Running into an issue while attempting to transmit data to the backend - receiving Uncaught (in promise) TypeError: Failed to fetch. Much obliged for any assistance. Backend: const path = require("path ...

Stroll and manipulate a multidimensional array using a pointer

I am facing a challenge with non-human-readable URLs on my website. I need to parse the URLs for each product, category, and brand that are listed. This task requires me to manipulate a multidimensional array by adding nodes. Below is the array I am worki ...

Enhancing D3 visualization with centralized alignment and mobile-friendly responsiveness

I am still quite new to JavaScript and development in general, so I may be overlooking something obvious. Although I have successfully created a chart using d3, I am struggling with positioning. No matter how much I manipulate it with CSS, the chart just d ...

Playing out the REST endpoint in ExpressJS simulation

Suppose I have set up the following endpoints in my ExpressJS configuration file server.js: // Generic app.post('/mycontext/:_version/:_controller/:_file', (req, res) => { const {_version,_controller,_file} = req.params; const ...

Encountering an Uncaught Exception while trying to import a Vue component onto my website

I am facing an issue on my page where I am loading certain JavaScript files. The files being loaded are: Vue.js app.js The app.js file has been compiled using webpack and includes a NPM component from @chenfengyuan/vue-countdown. My goal is to display a ...

Obtaining JSON data created through the use of the stringify method

After retrieving data from a database, I stored it in an array. To convert this array into JSON format, I used the stringify method which resulted in the following JSON structure: var view_data = [{ f o o = " boo " , t e x t = "t e s t " }]; However, w ...