Google Script checking that the data within one column on one sheet is the same as another column in another sheet

回眸只為那壹抹淺笑 提交于 2021-02-05 11:13:57

问题


I am trying to create a Google Script to check that the data within one column on one sheet is the same as another column in another sheet, any ideas where I am going wrong?

Thanks :) Code below:

function myFunction() {
  //data from Salesworks
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sales Works TEST");
  var data_salesworks = sheet.getRange('D3:D121').getValues();

  //data from Google Form
  var sheet = SpreadsheetApp.getActive().getSheetByName("V3 New Form Responses");
  var data_sheet = sheet.getRange('N3:N121').getValues();


  if (data_salesworks = data_sheet){
    Browser.msgBox('Date Range Correct');
  }
    else{
      Browser.msgBox('Date Range Incorrect');
}
}

回答1:


When the value is retrieved using getValues(), the value is 2 dimensional array. Unfortunately, such arrays cannot be directly compared. So it requires to modify the comparison part. I would like to propose following 2 patterns for this situation.

Pattern 1 :

In this pattern, each array is converted to string and compared them.

function myFunction() {
  //data from Salesworks
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sales Works TEST");
  var data_salesworks = sheet.getRange('D3:D121').getValues();
  //data from Google Form
  var sheet = SpreadsheetApp.getActive().getSheetByName("V3 New Form Responses");
  var data_sheet = sheet.getRange('N3:N121').getValues();
  if (data_salesworks.toString() == data_sheet.toString()) {
    Browser.msgBox('Date Range Correct');
  } else {
    Browser.msgBox('Date Range Incorrect');
  }
}

Pattern 2 :

In this pattern, all elements in each array are compared using "for loop".

function myFunction() {
  //data from Salesworks
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sales Works TEST");
  var data_salesworks = sheet.getRange('D3:D121').getValues();
  //data from Google Form
  var sheet = SpreadsheetApp.getActive().getSheetByName("V3 New Form Responses");
  var data_sheet = sheet.getRange('N3:N121').getValues();
  var f = true;
  for (var i=0; i<data_salesworks.length; i++) {
    for (var j=0; j<data_salesworks[i].length; j++) {
      if (data_salesworks[i][j] != data_sheet[i][j]) {
        f = false;
      }
    }
  }
  if (f) {
    Browser.msgBox('Date Range Correct');
  } else {
    Browser.msgBox('Date Range Incorrect');
  }
}

Note :

These samples are required to satisfy the following conditions.

  • The number of rows for both comparing data is the same.
  • The number of columns for both comparing data is the same.

Reference :

  • getValues()

If my understanding for your question is wrong, please tell me. I would like to modify it.




回答2:


Short answer

... where I'm doing wrong?

One error in your script is on data_salesworks = data_sheet because the single = is used to assign an object to a variable. The other error is assuming that an array could be compared directly.

Extended answer

Google Apps Script is based on JavaScript.

getValues() returns a 2 dimensional array which each member is a JavaScript primitive object according to the cell value and type, in other words, getValues() could return an array containing a combination of strings, dates, numbers and booleans.

On JavaScript, comparing two arrays directly doesn't work. For further details see How to compare arrays in JavaScript?

Considering the above, to compare two Google Spreadsheet ranges, first we could compare if the ranges sizes are equal and if so compare each innerst member of the first 2D array to the corresponding member of the second 2D array.

On JavaScript there are two equality operators, the abstract equality operator == and the strict equality operator ===. We should use the strict equality operator in order to differentiate something like '2 (the number 2 as string) from 2 (the number 2 as number).

Example:

The following script includes two functions. The first sets the address of the spreadsheet ranges to be compared, the second makes the comparisons.

Limitations: This script doesn't compare cell and content formatting, notes, and data validations, just values.

function test(){
  var address1 = 'A1:A3';
  var address2 = 'B1:B3';
  var output = compareRanges(address1,address2);
  Logger.log(output);

}

function compareRanges(address1,address2){
  var sheet = SpreadsheetApp.getActiveSheet();
  var values1 = sheet.getRange(address1).getValues();
  var values2 = sheet.getRange(address2).getValues();
  if(values1.length === values2.length && values1[0].length === values2[0].length) {
    for(var i = 0; i < values1.length; i++){
      for(var j = 0; j < values2.length; j++){
        if(values1[i][j] !== values2[i][j]) {
          return false;
        }
      }
    }
  } else {
    return false;
  }
  return true;
}


来源:https://stackoverflow.com/questions/47812317/google-script-checking-that-the-data-within-one-column-on-one-sheet-is-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!