How To: Replace Googles deprecated DocsList API service

Google has recently deprecated the DocsList API and replaced it with DriveAPP. The best way to show you how replace one with the other is to show you a before (orange) and after (green) code example. Please note the below code returns a copy of a document to a predetermined target folder using the original document name.

/* -------------------------------------------------------------------------------
 *
 * ©© Except where otherwise noted, this content is licensed, under a Creative
 * Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
 * http://creativecommons.org/licenses/by-nc-sa/3.0/
 *
 * Project Name: Duplicate Document
 *
 * Options: ---
 * Requirements: ---
 * Bugs: ---
 * Notes: ---
 * Author: Damir Knezev
 * Version: 1.1
 * Created: May 22 2014
 * Revision: 1.0 Created initial duplicate document script
 *           1.1 Replaced deprecated DocsList API service
 * Description: Google App Script to create a duplicate document including the
 * source document name.
 *
 * ---------------------------------------------------------------------------- */

function createDuplicateDocument(documentId, documentName) {
  // var oldFile = DocsList.getFileById(documentId);
  var oldFile = DriveApp.getFileById(documentId);

  var newFile = oldFile.makeCopy(documentName);

  // var targetFolder = DocsList.getFolderById(TARGET_FOLDER);
  // newFile.addToFolder(targetFolder);

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
  targetFolder.addFile(newFile);


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


No comments:

Post a Comment