【问题标题】:How to find multiple column number [duplicate]如何查找多个列号[重复]
【发布时间】:2021-12-27 19:31:41
【问题描述】:

我正在寻找包含关键字 1 的列号,然后是关键字 2 的列号,等等...

我找到了获取单列数的脚本,但是找不到多个关键字的解决方案

目标是找到我的脚本所涉及的列数,以避免在我删除工作表中的列时重写它们。

提前感谢您的帮助

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    有多种方法可以做到这一点。这是一个示例:

    工作表第 1 行的示例标题:数据:

    A B C
    Name Age Hobby

    脚本

    function headersExample(){
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet = ss.getSheetByName('Data');
      const headers = sheet.getRange(1,1,1,sheet.getLastColumn())
        .getValues()
        .flat()
        .reduce((acc, next, i) => {
          acc[next] = i + 1;
          return acc;
        },{})
    
      console.log(headers);
      console.log(`Column number for Hobby = ${headers["Hobby"]}`);
    }
    

    日志 1:

    { Name: 1, Age: 2, Hobby: 3 }
    

    日志 2:

    Column number for Hobby = 3
    

    【讨论】:

    • 非常感谢 RemcoE33 !我的脚本现在可以工作了;)
    猜你喜欢
    • 2011-12-30
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2014-03-20
    • 2022-01-10
    • 2018-03-11
    相关资源
    最近更新 更多