简体   繁体   English

将工作表 1 与工作表 2 进行比较并输出至工作表 3。Google 工作表。 JavaScript

[英]Compare sheet 1 to sheet 2 and output to sheet 3. Google sheets. JavaScript

The following code works successfully to compare column A of sheet 2 with Column B of sheet 1, any matches will copy the entire row to sheet 3. However im needing a very slight change of this code that compares column A of sheet 2 to column N of sheet 1 instead of column B. Could someone help me with this code change?以下代码成功地将工作表 2 的 A 列与工作表 1 的 B 列进行比较,任何匹配项都会将整行复制到工作表 3。但是,我需要对此代码进行非常细微的更改,以便将工作表 2 的 A 列与 N 列进行比较表 1 而不是 B 列。有人可以帮我更改此代码吗? Here is the link to the previous post Java script optimization for a google apps script这是上一篇文章的链接, 用于谷歌应用程序脚本的 Java 脚本优化

 function copyRowtoSheet3() {
  var spreadsheetId = "1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE";
  var ss = SpreadsheetApp.openById(spreadsheetId);
  var s1 = ss.getSheetByName('Sheet1');
  var s2 = ss.getSheetByName('Sheet2');
  // 1. Retrieve values from "Sheet1" and "Sheet2",
  var values1 = s1.getDataRange().getValues();
  var values2 = s2.getRange(1, 1, s2.getLastRow(), 1).getValues();

  // 2. Create an object using values2.
  var obj = values2.reduce((o, [e]) => {
    o[e] = null;
    return o;
  }, {});

  // 3. Create resultArray using values1 and obj.
  var resultArray = values1.filter(([,b]) => b in obj);

  // 4. Put resultArray to Sheet3.
  Sheets.Spreadsheets.Values.update({values: resultArray}, spreadsheetId, "Sheet3", {valueInputOption: "USER_ENTERED"});
}

What I have attempted is:我尝试过的是:

var resultArray = values1.filter(([,n]) => n in obj);

However that did not work.然而这并没有奏效。 Any ideas?有任何想法吗?

  • You want to retrieve the values of "Sheet1" and "Sheet2".您想检索“Sheet1”和“Sheet2”的值。
  • You want to compare the column "N" of "Sheet1" and the column "A" of "Sheet2".您想比较“Sheet1”的“N”列和“Sheet2”的“A”列。 When the values of the column "N" of "Sheet1" and the column "A" of "Sheet2" are the same, you want to retrieve the row of "Sheet1" and put to "Sheet3".当“Sheet1”的“N”列和“Sheet2”的“A”列的值相同时,您要检索“Sheet1”的行并放入“Sheet3”。
  • You want to achieve this by modifying your script.您想通过修改脚本来实现这一点。

Modification point:修改点:

  • var resultArray = values1.filter(([,b]) => b in obj); is modified.被修改。 In your current script, the column "B" from [,b] is compared.在您当前的脚本中,比较了[,b]的列“B”。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From: 从:
 var resultArray = values1.filter(([,b]) => b in obj);
To: 到:
 var resultArray = values1.filter(b => b[13] in obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM