【问题标题】:Trying to copy comments from one google sheet to a copy of the sheet in another location using apps script尝试使用应用程序脚本将评论从一个谷歌工作表复制到另一个位置的工作表副本
【发布时间】:2020-01-01 03:20:35
【问题描述】:
  • 试图将评论从一个 google 工作表复制到另一个位置的工作表副本。
  • 似乎没有 copyTo(),所以我正在尝试 moveTo()
  • 寻找更多想法。

在下面的代码中包含这个

它在同一个工作簿中工作:

// This works
function copyCellAndComments() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source_Sheet");
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Destination_Sheet");
  sheet.getRange("P21:26").moveTo(targetSheet.getRange("P21:26"));
};
// I run into permissions issues if I try something like
function copyCellAndComments02() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source_Sheet");
  //This didn't work.  Told me formatted wrong
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spreadsheet URL", "Sheetname");
  // I tried using SpreadsheetApp.openById() as well as SpreadsheetApp.openByUrl() in the script and it told me I don't have sufficient permissions to run openById or openByUrl.
    // (Error
Exception: You do not have permission to call SpreadsheetApp.openByUrl. Required permissions: https://www.googleapis.com/auth/spreadsheets )
  // Also tried creating a standalone function and calling the function within the sheet.
function importComments(spreadsheetKey,a1NotationReference) {
    return SpreadsheetApp.openByUrl(spreadsheetKey).getRange(a1NotationReference).getComments();
};

在网上找到了一些东西,说如果我添加一个菜单并从菜单中调用它就会运行。

     // That generates an Error - Exception: Invalid argument: url (line 7, file "MyScripts_RickP")

我尝试了 importrange。 Thant 复制评论中的文本,但不作为评论。

还尝试将其作为围绕导入范围的查询来执行。结果一样。

这是我要复制的测试评论

  sheet.getRange("P6").moveTo(targetSheet.getRange("P6"));
};

因为我还没有找到一种方法将带有完整 cmets 的工作表作为评论(评论文本复制好,但我需要将其作为评论复制)复制到存档位置。

【问题讨论】:

    标签: google-apps-script google-sheets-formula


    【解决方案1】:

    问题

    1. getSheetbyName(name) 只允许您从同一个电子表格中检索工作表,并且要求您仅指定工作表名称,而不指定电子表格 URL
    2. 已弃用用于电子表格的方法 getComments()。此功能有一个feature request,但目前尚未实现

    解决方法

    • 如果你想copy a sheet 到另一个电子表格中,你可以这样做:
      var targetSheet=SpreadsheetApp.openById('SPREADSHEETID');
    SpreadsheetApp.getActive().getSheetByName("Source_Sheet").copyTo(targetSheet);
    

    但是,这不会复制 cmets。

    • 同样适用于其他复制方法,如 SpreadsheetApp.getActive().copy('This is a copy');DriveApp.getFileById(SpreadsheetApp.getActive().getId()).makeCopy();
    • 您可以使用Advanced Drive Service 检索 cmets:

    var comments=Drive.Comments.list(SpreadsheetApp.getActive().getId());

    • 您可以使用检索到的评论在目标文件中创建评论,例如:

    Drive.Comments.insert({"content":comments.items[0].content, "context":comments.items[0].context, "author":comments.items[0].author}, targetSheet.getId());

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多