0

When I make a copy of an existing file with DriveApp, the DocumentApp is not able to find it.

My primary goal is to add some permissions to a file that was created by copying a template. Unfortunately by using DriveApp an email message is sent to users. When using the Document API this does not happen.

function copyTemplate() {
  var templateId = "xxxxxxx";
  var copy = DriveApp.getFileById(templateId).makeCopy("copy");
  var document = DocumentApp.openById(copy.getId());
  document.addEditor("xy@mail.com");
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Your code works fine for me, all I did was put in a valid document ID. Are you sure that the template is a DOCUMENT and not a SPREADSHEET? If it's a spreadsheet, you need to use SpreadsheetApp instead. See [this answer](http://stackoverflow.com/a/29279546/1677912). – Mogsdad Apr 23 '15 at 18:50
  • This is it. I thought DOCUMENT is a synonym for file. Thanks. When you post an answer I can vote it up. – user2725152 Apr 24 '15 at 04:47

2 Answers2

1

Both spreadsheets and documents have openById() methods, however they are filetype-specific. If you use DocumentApp.openById(), it must be given the ID of a google doc, not a spreadsheet.

Here's a utility that can handle both types for you.

/**
 * Create a new document or spreadsheet by copying a template, and give editing privileges
 * to the given editor.
 *
 * @param {string} templateId    ID of a spreadsheet or google doc, to be used as a template.
 * @param {string} filename      New file name.
 * @param {string} editor        (optional) Email address of user to be given editor privileges.
 */
function copyAndShareTemplate(templateId,filename,editor) {
  // If no editor provided, assume script owner
  var editor = editor || Session.getActiveUser().getEmail();

  // Make a copy of given template, as filename
  var copy = DriveApp.getFileById(templateId).makeCopy(filename);

  // Type-specific handling for spreadsheets & documents
  var filetype = copy.getMimeType();
  switch (filetype) {
    case MimeType.GOOGLE_DOCS:
      var document = DocumentApp.openById(copy.getId());
      break;
    case MimeType.GOOGLE_SHEETS:
      document = SpreadsheetApp.openById(copy.getId());
      break;
    default:
      throw new Error( "Unsupported document type." );
  }
  // Add editing permissions to doc or spreadsheet.
  // Note: the addEditor method exists for both.
  document.addEditor(editor);
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
-1

Trough the DriveApp it will never send, you'll have to send an Email manually with MailApp, or enable Drive advanced services, and use Permission.Insert inside Apps Script.

Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • The problem is that the DriveApp it will always send an email. So I chose to use DocumentApp so that no email is sent. However the source problem is, why DocumentApp can't find the file. – user2725152 Apr 23 '15 at 15:21
  • This doesn't answer the question; the OP isn't asking how to send an email wrt sharing. – Mogsdad Apr 23 '15 at 18:42