Script for a Google Doc .makeCopy() the comments a

2020-03-05 06:53发布

问题:

Hello is possible twhen copying a Google Doc document to copy also the comments in the "copy doc."Because I've tried this with the TEMPLATE_DOC_ID which has many comments and I don't find the comments in the "copy".I am missing something?It's another method? Thanks!

//Make a copy of the template file
        var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();

回答1:

Unfortunately, the Google Docs copied by makeCopy() don't include the comments. So the comments and replies are required to be inserted to the copied file, after the file was copied. In order to implement this, please enable Drive API at Advanced Google Services and API console.

Enable Drive API v2 at Advanced Google Services

  • On script editor
    • Resources -> Advanced Google Services
    • Turn on Drive API v2

Enable Drive API at API console

About Drive API, in your environment, this might have already been enabled.

  • On script editor
    • Resources -> Cloud Platform project
    • View API console
    • At Getting started, click Enable APIs and get credentials like keys.
    • At left side, click Library.
    • At Search for APIs & services, input "Drive". And click Drive API.
    • Click Enable button.
    • If API has already been enabled, please don't turn off.

Sample script :

var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();

// Added script
var commentList = Drive.Comments.list(TEMPLATE_DOC_ID);
commentList.items.forEach(function(item) {
  var replies = item.replies;
  delete item.replies;
  var commentId = Drive.Comments.insert(item, documentId).commentId;
  replies.forEach(function(reply) {
    Drive.Replies.insert(reply, documentId, commentId).replyId;
  });
});

Note :

  • Unfortunately, the create time and modified time couldn't updated. So the date becomes the created date.

References :

  • Advanced Google Services
  • Drive API
  • Comments: insert
  • Replies: insert

If this was not what you want, I'm sorry.