比较列

时间:2017-09-09 07:35:14

标签: javascript arrays google-apps-script

我试图扩展此工作脚本,以便在复制之前检查匹配列。

function copyInfo() // https://stackoverflow.com/questions/44967086/copy-data-from-one-sheet-to-another-in-google-app-script-and-append-a-row-one-s
{
  var copySheet = ssA.getSheetByName("export");
  var pasteSheet = ssA.getSheetByName("paste");
  var compareFrom = copySheet.getRange('B2:B99');
  var compareTo = pasteSheet.getRange('C2:C99');
  // if(compareFrom == compareTo){
    var source = copySheet.getRange('N1:O99'); // get source range
    var destination = ss.getRange('O1:P99'); // get destination range
    source.copyTo(destination); // copy values to destination range
    source.clearContent(); // clear source values
  // } else{SpreadsheetApp.getUi().alert("rows don't match!");}
}

基本上,我想检查源表中B列中的值是否与粘贴表C列值的顺序相同。

1 个答案:

答案 0 :(得分:0)

我认为这应该有所帮助。你错过了几个步骤,我试着在评论中澄清。

function copyInfo() // https://stackoverflow.com/questions/44967086/copy-data-from-one-sheet-to-another-in-google-app-script-and-append-a-row-one-s
{
  //you need to get the SpreadsheetApp service
  var copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("export");
  var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("paste");

  //get the values from the ranges and flatten with reduce
  var compareFrom = copySheet.getRange('B2:B99').getValues().reduce(function(a, b) {return a.concat(b);});
  var compareTo = pasteSheet.getRange('C2:C99').getValues().reduce(function(a, b) {return a.concat(b);});

  //you need to loop over the compare from array and indexOf the compareTo array
  for(var i = 0; i < compareFrom.length; i++) {
    var inCompareTo = compareTo.indexOf(compareFrom[i]);

    if(inCompareTo > -1) {
      var source = copySheet.getRange('N1:O99'); // get source range
      var destination = ss.getRange('O1:P99'); // get destination range
      source.copyTo(destination); // copy values to destination range
      source.clearContent(); // clear source values
    }
  }
}