【问题标题】:Google Sheet App Script: Match two values from one sheet to another sheet and then set a value if they matchGoogle Sheet App Script:将一个工作表中的两个值匹配到另一个工作表,然后在它们匹配时设置一个值
【发布时间】:2022-11-09 20:24:47
【问题描述】:

对应用程序脚本非常陌生,但精通公式和技能提升。

作为参考,我制作了一张测试表HERE

我的脚本一直是教程的科学怪人,但基本上我正在尝试将工作表 2 中的名称和日期与工作表 1 中的数据相匹配,如果它们匹配,那么在“C”列中我想将值设置为“已发送”

这是我迄今为止尝试过的:

    function sendReport() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var clientName = ss.getRange ("Sheet2!B1");
  var testDate = ss.getRange ("Sheet2!B2")
  var destSheet = ss.getSheetByName("Sheet1");
  var range = destSheet.getDataRange();
  var values = range.getValues();
  for(var i = 1; i < values.length; i++){

    if (values[i][1] == clientName
        && values[i][2] == testDate) {
      values[i][3] = Yes; 
}
range.setValues(values)
  }

实际工作表非常大,它似乎一次检查每一行,因为脚本需要很长时间才能运行,但是它没有在列中添加“是”值以确认报告已发送。

非常感谢任何帮助。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您可以使用以下脚本

    function sendReport() {
      Logger.log("Starting script...");
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      // you need to get the actual sheets of the array
      // you can use array destructuring (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) for this
      const [sheet1, sheet2] = ss.getSheets(); // getSheets() returns an array of sheets
      // get ranges from sheet 2
      // no need to provide the sheet name as a parameter, because we're calling it on sheet2 already
      const clientName = sheet2.getRange("B1").getDisplayValue(); // you need to get the display value, not just the range
      const testDate = sheet2.getRange("B2").getDisplayValue(); 
    
      // start in row 2, column 1 and get all rows expect the first (=> last - 1) and get 3 columns
      const range = sheet1.getRange(2, 1, sheet1.getLastRow() - 1, 2);
      // use forEach() and array destructuring again here. 
      // the second parameter idx is a zero-based index we need to set the value of column sent 
      range.getDisplayValues().forEach(([name, date, sent], idx) => {
        // Use JavaScript strict comparison with === instead of ==
        // check if name and date match and sent is not already set to "sent"
        Logger.log(`Comparing name ${name} with ${clientName} and date ${date} with ${testDate}...`);
        if(name === clientName && date === testDate && sent !== "sent") {
          // we need to use idx + 2 because line 1 is the heading and idx of forEach() is zero-based but Spreadsheet API is one-based
          Logger.log(`Ìn row ${idx + 2} name and date match! Setting "sent"...`);
          sheet1.getRange(idx + 2, 3).setValue("sent");
        }
      })
      // set value "report sent" on sheet2  to "sent"
      sheet2.getRange("B3").setValue("sent")
      Logger.log("Report sent.")
    }
    

    关于您的代码的一些指示。

    • 您需要在Range 上使用getValue()getDisplayValue() 获取实际值,以便获取这些值。
    • 你可以使用destructuring,它比一直处理索引更方便、更易读
    • 您应该使用forEach 或其他JavaScript 数组方法来迭代数组
    • JavaScript 有多个比较运算符。您应该尽可能使用strict equality operator ===,因为与normal equality operator == 相比,这也会考虑数据类型
    • 您可以在脚本执行时使用Logger.log() 将某些内容打印到控制台
    • 尝试使用const 变量而不是var 来更改变量的值。最好的做法是根本不更改值,而是将它们视为immutable,因为这样可以避免很多错误。

    输入

    在 Sheet1 中输入

    我已经更改了一行(在我的电子表格副本中),以便一个元素实际上满足条件并被更新。

    在 Sheet2 中输入

    结果

    输出表1

    输出表2

    【讨论】:

    • 非常感谢您详细介绍。效果很好,花了一点时间将它从简单的测试表变成真实的东西,但效果很好。再次感谢你。我是一名运动生理学家,并且最大限度地利用了目前公式可以为我做的事情,并且我需要代码。
    【解决方案2】:

    嗨,我有一个类似的问题,但有更多的标准。你能帮我吗?

    我有两个电子表格,第一个电子表格 - https://docs.google.com/spreadsheets/d/1yhJDVKQHJmcFp8Fgo7fXwpeR_g6yr8ld3uHpFRWt3qo/edit?resourcekey=undefined#gid=2070523819

    从这个 ss 我想将名称 (ColB) 和日期 (ColC) 与电子表格两个匹配 - name(ColA) , date(ColP)

    链接-https://docs.google.com/spreadsheets/d/1PxQ_ueIldgiiLazj_dCdixk3VgEs0e87mugPR2QIihU/edit#gid=0

    如果它们匹配,那么我想从 Col 'D' 电子表格一中复制数据并将其值粘贴到电子表格二中的 col 'Q' 中。

    这两个电子表格是自动化的。请帮我写代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多