如何使用谷歌电子表格文件中的所有评论创建报告

问题描述 投票:0回答:1

我有一个 Google 电子表格,我与公司内部的合作者共享。 Google 电子表格包含 30-31 张表格(一张代表每月的每一天)。

工作表内的“A”列有职位描述,“D”列有一个下拉列表。

“D”栏包含协作者的所有评论。

有没有一种方法可以根据请求自动使用脚本(分配给按钮)在同一电子表格文件内的新工作表上创建报告,其中包含“A”列中的所有描述以及“D”列中的相应注释从每张纸上写下来?

google-sheets google-apps-script comments
1个回答
0
投票

问题和解决方法:

现阶段,还没有内置方法可以直接从 Google Spreadsheet 上的每个单元格中检索评论。因此,为了实现这一目标,我创建了一个 Google Apps 脚本库DocsServiceApp(作者:我)。在这个答案中,使用了该库。

用途:

1.安装库

您可以在here查看如何安装此库DocsServiceApp。

2.示例脚本

使用您提供的电子表格时,示例脚本如下。当我看到您的电子表格时,我确认评论已放入“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”表中,如下所示。看来这个结果和你预期的输出是一样的。

注:

  • 此示例脚本适用于您提供的电子表格。当您更改电子表格时,此脚本可能无法使用。请注意这一点。因此,请在您提供的电子表格上测试此脚本。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.