【问题标题】:How to modify arrayFormula and vlookup in Google Apps Script如何在 Google Apps 脚本中修改 arrayFormula 和 vlookup
【发布时间】:2023-01-02 15:41:59
【问题描述】:

您好,我是 Google Apps 脚本的新手。我想将下面的这个公式修改为 Google Apps 脚本

=arrayFORMULA(iferror(VLOOKUP(J3,{'WO SR 22/23'!P:P,'WO SR 22/23'!B:B},2,FALSE)))

查找值位于 wsPetitionStatusReport 表中。我尝试了下面的代码,但它总是返回 null。你能帮我吗?太感谢了

function vLookUpVALUE() {

   const ss = SpreadsheetApp.getActiveSpreadsheet();
const wsWOSR = ss.getSheetByName("WO SR 22/23")
const wsPetitionStatusReport  = ss.getSheetByName("Petition Status Report ")
 
const searchVALUES = wsPetitionStatusReport.getRange("J3:J").getValues()


const wsWOSRDATcolO = wsWOSR.getRange("B3:P" + wsWOSR.getLastRow()).getValues();


const matchDATAcolO = searchVALUES.map(searchROWcolO => {
        const matchROWcolO = wsWOSRDATcolO.find (r => r[0] == searchROWcolO[0])
        return matchROWcolO ? [matchROWcolO[0]] : [null]
    
})
  
 console.log(matchDATAcolO)


}

【问题讨论】:

    标签: google-apps-script google-sheets google-sheets-formula


    【解决方案1】:

    您可以通过将 J3 替换为开放式引用(如 J3:J)来计算整列的数组公式:

    =arrayformula(iferror(vlookup(J3:J, { 'WO SR 22/23'!P:P, 'WO SR 22/23'!B:B }, 2, false)))

    要对 Apps 脚本执行相同的操作,请使用如下内容:

    function vlookupLookAlike() {
      const ss = SpreadsheetApp.getActive();
      const source = {
        values: ss.getRange('WO SR 22/23!B3:P').getValues(),
        keyColumn: 0,
        dataColumn: 14, // =column(P3) - column(B3)
      };
      source.keys = source.values.map(row => row[source.keyColumn]);
      source.data = source.values.map(row => row[source.dataColumn]);
      const target = {
        range: ss.getRange('Petition Status Report !J3:J'), // trailing space
      };
      target.keys = target.range.getValues().flat();
      target.results = target.keys.map(key => [source.data[source.keys.indexOf(key)]]);
      console.log(target.results);
      // target.range.offset(0, 1).setValues(target.results);
    }
    

    【讨论】:

    • 你好。当我尝试运行您的代码时,它返回未定义。为什么?
    • 我给你的函数不返回任何东西——它写入控制台。您可以通过取消注释最后一行来写入电子表格。确保工作表名称正确。如果您的搜索关键字是日期或时间,请将 .getValues() 替换为 .getDisplayValues()。显示您的数据和结果。不要解释错误消息,而是逐字逐句地引用错误消息。
    猜你喜欢
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多