3

I am trying to have the newly copied document to be updated upon completion of being copied, added to a different folder and its root file removed. The Google script is not able to find the newly copied file. The error is in the lower half of the code surrounded by asterisks with TypeError.

I suspect it has to do with the line of var doc = DocumentApp.getActiveDocument();. I am not sure how to get the URL of the newly copied document (file2 or Date - Weekly Agenda).

  function copyupdateWeeklyAgenda() {
  var file = DriveApp.getFileById("FILEKEY");
  var source_folder = DriveApp.getFolderById("FOLDERKEY");
  var dest_folder = DriveApp.getFolderById("FOLDERKEY");
  // Make a backup copy.
  var file2 = file.makeCopy('Date - Weekly Agenda');
  dest_folder.addFile(file2);
  source_folder.removeFile(file2);

//START Update Weekly Agenda Data ---
  var ssUrl = 'https://whydoyouwanttoknow.com';
  var sheetName = 'Sheet1';   // name of sheet to use
  var rangeName = 'A1:B2';    // range of values to include 
  var values = SpreadsheetApp.openByUrl(ssUrl)
                             .getSheetByName(sheetName)
                             .getRange(rangeName)
                             .getValues();
  var doc = DocumentApp.getActiveDocument();
  **var body = doc.getBody();** <--- TYPEERROR: Cannot call method "getBody" of null.
  var ranges = doc.getNamedRanges('embeddedSheet-range');
  if (ranges.length == 0) {
    var table = body.appendTable(values);
  ...ETC.
Rubén
  • 34,714
  • 9
  • 70
  • 166
Mario Essig
  • 206
  • 1
  • 4
  • 15
  • Are FILEKEY and FOLDERKEY just there for an example and the IDs are actually there, such as 0B91nqXWdmtvc0xxcHpnMDA for the folder? If not, these are found in the url of each. Also, I donot believe spreadsheets have a body, so geBody may not be available for them. I may be wrong on that part. See the last lines of the code in the response to [ this question](http://stackoverflow.com/questions/31078758/google-app-script-how-do-i-make-copy-of-spreadsheet-and-save-it-to-particular-f) for copying a spreadsheet to a specific file. – Karl_S Dec 10 '16 at 01:49
  • The FILEKEY and FOLDERKey is a buffer for the actual IDs. The code performs these in order: 1) Copies the document template 2) Adds the copied document template to a different folder 3) Removes the root file 4) Update the copied document template by doing: 4a) Update the spreadsheet 4b) Insert table onto the copied document template using data from the spreadsheet 4c) performs the rest of the code (...ETC). – Mario Essig Dec 10 '16 at 01:52

1 Answers1

3

Your file2 variable is an object of class File, because this is what File#makeCopy returns. To get the corresponding Document, use

var doc = DocumentApp.openById(file2.getId());

Remarks

  1. The method getActiveDocument refers to the Document to which the script is attached (if any). It does not mean "the thing that the script is currently handling".

  2. If you were dealing with a spreadsheet, then var ss = SpreadsheetApp.open(file2) would return the Spreadsheet contained in file2. Apparently, the API designers forgot to include the open method in DocumentApp, so we have to go through the file ID as above.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • 1
    The code above, given by zaq, works perfectly. Thank you for elaborating on what it does and what I have done wrong. You are a champ. – Mario Essig Dec 13 '16 at 00:08