How can I replace the deprecated DocsList service?

2019-04-11 01:21发布

问题:

This question already has an answer here:

  • How to update DocsList to DriveApp in my code 2 answers

Since April 20, 2015 the DocsList service "service no longer functions". I have this function this function which used docsList service:

/**
 * Duplicates a Google Apps doc
 *
 * @return a new document with a given name from the orignal
 **/

function createDuplicateDocument(sourceId, name) {
    var source = DocsList.getFileById(sourceId);
    var newFile = source.makeCopy(name);

  var targetFolder = DocsList.getFolderById(TARGET_FOLDER);
    newFile.addToFolder(targetFolder);
    return DocumentApp.openById(newFile.getId());
}

How can I replace the DocsList service in this createDuplicateDocument function?

回答1:

Here is the new way:

function createDuplicateDocument(sourceId, name) {
  //var source = DocsList.getFileById(sourceId);
  var source = DriveApp.getFileById(sourceId);

  var newFile = source.makeCopy(name);

  //var targetFolder = DocsList.getFolderById(TARGET_FOLDER);
  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);

  //newFile.addToFolder(targetFolder);
  targetFolder.addFile(newFile);

  return DocumentApp.openById(newFile.getId());
};