如何使用Google表格中的Apps Script将所有评论转换为备注?

huangapple go评论68阅读模式
英文:

How to convert all comments to notes using appsscript in googlesheets?

问题

我想把Google表格单元格中的所有评论都转换为同一单元格中的备注。在这里,单元格中的数据不应更改。并且应该包括发表评论的人的姓名。
我使用了以下代码来实现第一个要求。但它没有起作用。如何正确地完成这个任务?

function convertCommentsToNotes() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getDataRange();
  var notes = range.getNotes();

  for (var i = 0; i < notes.length; i++) {
    for (var j = 0; j < notes[i].length; j++) {
      var note = notes[i][j];
      var comment = sheet.getRange(i + 1, j + 1).getComment();
      if (comment !== null) {
        var commentText = comment.getRange().getValue();
        note = note + "\n\n" + commentText;
      }
      sheet.getRange(i + 1, j + 1).setNote(note);
    }
  }
}
英文:

I want to get all the comments in google sheet cells to convert as a note in the same cell. Here the data in the cell should not change. And the name of the person who made the comment should also be included.
I used this code for the first requirement. But it didn't work. How can this be done correctly?

function convertCommentsToNotes() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getDataRange();
  var notes = range.getNotes();

  for (var i = 0; i &lt; notes.length; i++) {
    for (var j = 0; j &lt; notes[i].length; j++) {
      var note = notes[i][j];
      var comment = sheet.getRange(i + 1, j + 1).getComment();
      if (comment !== null) {
        var commentText = comment.getRange().getValue();
        note = note + &quot;\n\n&quot; + commentText;
      }
      sheet.getRange(i + 1, j + 1).setNote(note);
    }
  }
}

答案1

得分: 1

我相信您的目标如下。

  • 您想要使用Google Apps Script将注释转换为单元格中的备注。
  • 您想要将注释添加到现有的备注中。

问题和解决方法:

在当前阶段,为了从电子表格中检索注释,需要使用Drive API的注释。但是,即使从API中检索到了注释,它们的形式是 &quot;anchor&quot;: &quot;{\&quot;type\&quot;:\&quot;workbook-range\&quot;,\&quot;uid\&quot;:0,\&quot;range\&quot;:\&quot;123456789\&quot;}&quot;,而无法得知锚定单元格在\&quot;range\&quot;:\&quot;123456789\&quot;中的位置。

基于上述情况,我创建了一个用于获取单元格注释的Google Apps Script库。参考链接 在这个答案中,我想提出一个修改过的脚本,以便使用这个库。

使用方法:

1. 安装Google Apps Script库。

您可以在这里找到安装方法。

2. 启用Drive API。

请在高级Google服务中启用Drive API。参考链接

3. 示例脚本。

请将以下脚本复制粘贴到电子表格的脚本编辑器中。

function convertCommentsToNotes() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ssId = ss.getId();
  const sheet = ss.getActiveSheet();
  const notes = sheet.getDataRange().getNotes();
  const res = DocsServiceApp.openBySpreadsheetId(ssId).getSheetByName(sheet.getSheetName()).getComments();
  const obj = res.reduce((o, { range, comment }) => (o[`${range.row - 1}_${range.col - 1}`] = comment.map(e => e.comment.trim()).join("\n"), o), {});
  const maxRows = sheet.getMaxRows();
  const maxCols = sheet.getMaxColumns();
  const newNotes = [];
  for (let i = 0; i < maxRows; i++) {
    const temp = [];
    for (let j = 0; j < maxCols; j++) {
      const note = ((notes[i] && notes[i][j]) ? notes[i][j] : "") + (obj[`${i}_${j}`] || "");
      temp.push(note);
    }
    newNotes.push(temp);
  }
  sheet.getRange(1, 1, newNotes.length, newNotes[0].length).setNotes(newNotes);

  // 如果您想删除所有注释,也可以使用以下脚本。
  // 但是,请注意,当您使用以下脚本时,要小心,因为它会删除所有注释。
  // const commentIds = Drive.Comments.list(ssId).items.map(({ commentId }) => commentId);
  // commentIds.forEach(id => Drive.Comments.remove(ssId, id));
}
  • 当运行此脚本时,将检索Google电子表格活动工作表中的所有注释。然后,将这些注释添加到现有的备注中。

参考链接:

英文:

I believe your goal is as follows.

  • You want to convert the comments to cells as the notes to the same cells using Google Apps Script.
  • You want to add the comments to the existing notes.

Issue and workaround:

In the current stage, in order to retrieve the comments from Spreadsheet, Comments of Drive API is required to be used. But, even when the comments are retrieved from the API, it is like &quot;anchor&quot;: &quot;{\&quot;type\&quot;:\&quot;workbook-range\&quot;,\&quot;uid\&quot;:0,\&quot;range\&quot;:\&quot;123456789\&quot;}&quot; while the comment text can be obtained. Unfortunately, it cannot be known the anchor cell from \&quot;range\&quot;:\&quot;123456789\&quot;.

From the above situation, I created a Google Apps Script library for obtaining the comments from the cells. Ref In this answer, I would like to propose a modified script for reflecting this library.

Usage:

1. Install Google Apps Script library.

You can see how to install it at here.

2. Enable Drive API.

Please enable Drive API at Advanced Google services. Ref

2. Sample script.

Please copy and paste the following script to the script editor of Spreadsheet.

function convertCommentsToNotes() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ssId = ss.getId();
  const sheet = ss.getActiveSheet();
  const notes = sheet.getDataRange().getNotes();
  const res = DocsServiceApp.openBySpreadsheetId(ssId).getSheetByName(sheet.getSheetName()).getComments();
  const obj = res.reduce((o, { range, comment }) =&gt; (o[`${range.row - 1}_${range.col - 1}`] = comment.map(e =&gt; e.comment.trim()).join(&quot;\n&quot;), o), {});
  const maxRows = sheet.getMaxRows();
  const maxCols = sheet.getMaxColumns();
  const newNotes = [];
  for (let i = 0; i &lt; maxRows; i++) {
    const temp = [];
    for (let j = 0; j &lt; maxCols; j++) {
      const note = ((notes[i] &amp;&amp; notes[i][j]) ? notes[i][j] : &quot;&quot;) + (obj[`${i}_${j}`] || &quot;&quot;);
      temp.push(note);
    }
    newNotes.push(temp);
  }
  sheet.getRange(1, 1, newNotes.length, newNotes[0].length).setNotes(newNotes);

  // If you want to delete all comments, you can also use the following script.
  // But, when you use the following script, please be careful about this, because all comments are deleted.
  // const commentIds = Drive.Comments.list(ssId).items.map(({ commentId }) =&gt; commentId);
  // commentIds.forEach(id =&gt; Drive.Comments.remove(ssId, id));
}
  • When this script is run, all comments in the active sheet of Google Spreadsheet are retrieved. And, those comments are added to the existing notes.

Reference:

huangapple
  • 本文由 发表于 2023年3月9日 17:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定