我有一个 Google 电子表格,我与公司内部的合作者共享。 Google 电子表格包含 30-31 张表格(一张代表每月的每一天)。
有没有一种方法可以根据请求自动使用脚本(分配给按钮)在同一电子表格文件内的新工作表上创建报告,其中包含“A”列中的所有描述以及“D”列中的相应注释从每张纸上写下来?
现阶段,还没有内置方法可以直接从 Google Spreadsheet 上的每个单元格中检索评论。因此,为了实现这一目标,我创建了一个 Google Apps 脚本库DocsServiceApp(作者:我)。在这个答案中,使用了该库。
您可以在here查看如何安装此库DocsServiceApp。
使用您提供的电子表格时,示例脚本如下。当我看到您的电子表格时,我确认评论已放入“29/01”和“28/01”工作表的“D”列中。这也体现在你的问题中。
请将以下脚本复制并粘贴到 Google Spreadsheet 的脚本编辑器中并保存脚本。
在此脚本中,这些工作表
const excludeSheetNames = ['START', 'MASTER', 'REPORT']
被跳过。如果您想跳过另一张工作表,请将工作表名称设置为该数组。
function myFunction() {
const excludeSheetNames = ['START', 'MASTER', 'REPORT']; // Exclude sheet names.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const sheett = sheets.find(s => !excludeSheetNames.includes(s.getSheetName()));
const [head, , , ...rows] = sheett.getRange("A1:A" + sheett.getLastRow()).getValues();
const res = DocsServiceApp.openBySpreadsheetId(ss.getId()).getComments();
res.forEach(({ sheetName, comments }, h) => {
sheetName = sheets[h].getSheetName();
if (!excludeSheetNames.includes(sheetName)) {
head.push(sheetName);
rows.forEach((e, i) => {
const t = comments.find(f => i == f.range.row - 4);
e.push(t ? t.comment[0].comment.trim() : null);
});
}
});
const values = [head, ...rows];
const dstSheet = ss.getSheetByName("REPORT") || ss.insertSheet("REPORT");
dstSheet.clearContents().getRange(1, 1, values.length, values[0].length).setValues(values);
}
运行此脚本时,结果值将放入“REPORT”表中,如下所示。看来这个结果和你预期的输出是一样的。