Recommand · June 10, 2021 0

Apps Script – compare two arrays, find differences (value and index of cells)

I have two vertical 1D arrays, one with existing data, and another that could (potentially) have differences.

I want to be able to compare the two arrays, find the differences AND the index of each cell that is different.

Then, I would use that information to find the appropriate cells in the existing data that need to be changed, and set the new values in those specific cells, without having to copy and paste the entire array.

For example:

Existing Data New Data
John Smith John Smith
012345 012345
6th grade 7th grade
555-1234 555-1357
Trumpet Trumpet
5th period 2nd period
Jane Smith Jane Smith

In this case, the code would see that rows 3,4, and 6 have differences, save those new values and their places in the array, then update the appropriate values in the main data list without changing anything else.

I’ve tried multiple ways to compare the two arrays AND get the index of the rows that have differences and this is as far as I’ve gotten without an error or a ‘null’ result:

function updateInfo() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = ss.getSheetByName("CURRENT");
var infoSheet = ss.getSheetByName("INFO Search");

var origVal = infoSheet.getRange(5,2,52,1).getValues();
var newVal = infoSheet.getRange(5,4,52,1).getValues();
var list = [];

var origData =,index){
  return [row[0],index];

var newData =,index){
  return [row[0],index];

This just gives me the value and index of each cell.
Is there a fast and efficient way to compare the two arrays, get the data I need, and change the values in just certain cells of the original column?
I can’t just copy and paste the whole column over because there are formulas embedded in various rows that need to remain intact.

Thanks for any help you can provide!


Iterate through the new data array. For each value, check whether the new value matches the old one. If that’s the case, add the corresponding value and index to your list.

Then iterate through your list, and for each pair of value and index, write the value to the corresponding cell.

Code snippet:

newVal.forEach(function(row, index) {
  if (row[0] === origVal[index][0]) {
    list.push([row[0], index]);
var firstRow = 2; // Change according to your preferences
var columnIndex = 1; // Change according to your preferences
list.forEach(data => {
  sheet.getRange(data[1] + firstRow, columnIndex).setValue(data[0]);


  • I don’t know where should the data be written, so I’m using undefined sheet, and also random values for firstRow and columnIndex. Please change these according to your preferences.
  • It would be much more efficient, from a script perspective, to write the whole column to your destination range at once, since that would minimize the number of interactions between the script and the spreadsheet (see Use batch operations). Since you want to write only the updated data, though, I provided a script that does this.