55177

Get document's URL/ID after a file was copied

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.

Answer1:

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

<ol> <li>

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".

</li> <li>

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.

</li> </ol>

Recommend

  • What does “Service error: Spreadsheets” mean?
  • PhoneGap chunckedMode true upload error
  • How to build WebClient querystring with duplicate keys?
  • Access denied to files after migration from parse.com to heroku
  • Copy, rename and move a document
  • How to remove a parent from a file or folder in google app script
  • Script runtime quota exceeded when copying files [duplicate]
  • Google Script for Uploading File Not Working
  • Copy entire folder structure in Powershell without re-creating root folder
  • DocumentApp.openById() fails with “Service unavailable”
  • App Script: How to view PDF of Google Sheet after function to save to Google Drive?
  • Add user ID to PhoneGap File Upload
  • Filter Values of Current Week with XQuery
  • Can't upload base64 image using cordova-plugin-file-transfer
  • Pass by Value and Pass by Reference
  • Upgrade from Jenkins Weekly to LTS
  • DriveApp conversion from DocX to PDF fails (google apps script)
  • Parallel sieve of Eratosthenes - Java Multithreading
  • LINQ join with filter criteria
  • How to get month name with year and list of years between two Date
  • Is there a way to disable a specific event in kendo ui scheduler?
  • how can I compare dates in array to find the earliest one?
  • Count from each distinct date, fill in missing dates with zero
  • How to validate a year I enter in textbox using jquery rule?
  • How to request unsampled report from Google Analytics Management API using Google Apps Script?
  • Identifier too long in Oracle
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Easiest way to get current unix timestamp via XSL
  • HttpListener.IsSupported is false on XP SP3
  • How to get data from **Realm database** using **date object**?
  • Saving image to sd with current date and time in name doesn't work
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Combining two different ActiveRecord collections into one
  • Stop Bash Script if Hive Fails
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Illegal mix of collations for operation for date/time comparison
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query