【发布时间】:2021-04-18 16:57:28
【问题描述】:
假设我有一个 masterSheet,名称在 A 列,在 B 列有一个链接,从第 2 行开始。 我希望在 masterSheet 中添加的每一行都自动添加:
- 创建在 A 列中包含人员姓名的新工作表
- 在这个新创建的工作表中设置 A 和 B 的值
- 在 C 中设置我的 IMPORTRANGE 公式
- 在 J2:J50 中设置我的 CONCATENATE 公式
第 1 点和第 2 点运行良好。第 3 点和第 4 点根本不起作用,我收到一条类似这样的错误消息:TypeError: sheetNames.getRange is not a function
我无法弄清楚这有什么问题,但我很确定它来自我将它们放入循环或其他东西的地方。
有人可以帮我解决这个问题吗?
function createNewSheets() {
// 1. Retrieve the current sheet names.
var dataSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = dataSheet.getSheets().map(s => s.getSheetName());
// 2. Retrieve the values from "mastersheet" sheet.
var masterSheet = dataSheet.getSheetByName('Liste de nageurs');
var values = masterSheet.getRange('A2:B' + masterSheet.getLastRow()).getValues();
// Imports table from link
var swimmerTable = '=IMPORTHTML(B1;"table";4)'
// Concatenates event with pool size
var fullName = 'CONCATENATE(C2;" - ";D2)'
// 3. Using the retrieved sheet names and values, the new sheets are inserted and the values are put.
values.forEach(r => {
if (!sheetNames.includes(r[0])) {
dataSheet.insertSheet(r[0]).appendRow(r);
sheetNames.push(r[0]);
// Sets the formula in the 3rd column:
sheetNames.getRange(1, 3).setFormula(swimmerTable)
// Sets the formula2 and iterates it from row 2 to row 50
sheetNames.getRange("J2:J50").setFormula(fullName)
}
});
}
【问题讨论】:
标签: loops google-apps-script google-sheets foreach google-sheets-formula