Google表格中多个表格的一项功能 [英] One function for multiple sheets in Google Sheets

查看:64
本文介绍了Google表格中多个表格的一项功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google表格中,我必须重复一个功能,因为 getSheetByName()不接受表格数组,它仅接受一个表格.

有没有一种方法可以循环遍历指定的工作表(不是所有工作表)?


("Sheet1","Sheet2")等.

in Google Sheets I have to repeat a function because getSheetByName() does not accept an array of sheets, it only accepts one sheet.

Is there a way to have one function that loops through specified sheets (not all sheets)?

i.e.
("Sheet1", "Sheet2" ) etc.

function recordHistory_1() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName("Sheet1");
        var source = sheet.getRange("A2:B2");
        var values = source.getValues();
        values[0][0] = new Date();
        sheet.appendRow(values[0]);
};

function recordHistory_2() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName("Sheet2");
        var source = sheet.getRange("A2:B2");
        var values = source.getValues();
        values[0][0] = new Date();
        sheet.appendRow(values[0]);
};

我问的原因是因为我有20多张纸,所以我必须写20次此函数...

Reason I'm asking is because I have over 20 sheets, and so I have to write this function 20 times...

推荐答案

您还可以调用一次函数,并在函数内部遍历一系列工作表名称.

You can also call the function once and loop through an array of sheetnames inside the function.

function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
["Sheet1", "Sheet2", "Sheet3"].forEach(function (s) {
    var sheet = ss.getSheetByName(s);
    var values = sheet.getRange('A2:B2').getValues()
    values[0][0] = new Date();
    sheet.appendRow(values[0]);
})
}

这篇关于Google表格中多个表格的一项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆