首页 文章

从多张Google表格中获取值

提问于
浏览
3

我正在尝试从考勤中找到一个员工ID,该员工ID跨越多张工作表并将时间戳拉入单张工作表 .

Google表格中有多张表格 . 每个工作日都有单独的床单:
Daily sheets

每张考勤表都有两列 . 为了了解员工登录的所有时间,我想从所有工作表中提取员工ID的所有出现次数以及时间戳到合并表:
Consolidation sheet
.

我在Excel中做了类似的事情,猜测可以使用Google Apps脚本在Google表格中完成 .

如果有人可以引导我使用google工作表中的内置或自定义功能,将会很有帮助 .

1 回答

  • 1

    我将帮助你提供一个基本的大纲,一些建议和一些资源来帮助你,但是不要指望我写下整件事 .


    Step 1 - Create a custom menu option

    你'll want to be able to access you script from the spreadsheet. You can accomplish this by creating a custom menu option. Here'是article by google on custom menus .

    虽然谷歌使用简单的触发器 onOpen ,但我发现installable triggers更可靠 .

    Step 2 - Get user input

    提示输入员工的身份并让脚本发挥作用是很好的 . 这是一个article by google on dialogs and sidebars,讨论如何获取脚本的用户输入 .

    Step 3 - Read and write data from the spreadsheet

    您可以使用SpreadsheetApp访问spreadsheeet数据 . 从自定义菜单调用脚本时,可以使用 SpreadsheetApp.getActiveSpreadsheet ;但是,如果要在脚本编辑器中测试代码,则没有"active spreadsheet",因此请改用 SpreadsheetApp.openById .

    通过访问电子表格,您可以致电:

    spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).getValues();
    spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).setValues([[1, 2], [3, 4]]);
    

    请注意 getValuessetValues 使用二维数组

    警告 - 像往常一样,I / O需要大量的处理时间,所以要避免多余调用 getRange().XetValues ,这个google article about appscript best practices会更详细 . 此外,如果您有很多考勤表,那么您可能需要考虑将它们合并为一个 .

    Step 4 - Get the appropriate sheets

    您需要在某种程度上区分电子表格中的哪些工作表是考勤表:

    function isAttendanceSheet(sheet) {
      return /Magical regex/.test(sheet.getName);
    }
    
    var sheets = spreadsheet.getSheets().filter(isAttendanceSheet);
    

    想出适合你的魔法正则表达式,这将过滤它们 .


    希望这有帮助,祝你好运!

相关问题