Google App Script Unexpected error while getting the method or property getFileById on object DriveApp

Total
3
Shares

I have this code to export my Google sheets to a ‘CSV’ delimited tab "|" thanks to the help @iansedano.

So this is the script:

function createCsvContent(values, delimiter) {

  // This converts the values 2D array into a simple array of strings delimited by the delimiter
  const stringArray = values.map(row => row.join(delimiter))
  // Then it joins all the strings with newlines
  const output = stringArray.join("n")

  return output
}


function createCsv() {
  // Get all the values from the sheet as a 2D array
  const file = SpreadsheetApp.getActive();
  const sheet = file.getSheetByName("LIST");
  const range = sheet.getDataRange();
  const values = range.getValues();

  // call the function to create the csv-like content
  const csvContent = createCsvContent(values, "|")

  // create the file in drive
  //DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
  DriveApp.getFileById("[10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y]").setContent(csvContent)
}

So each time I run the script, I want to overwrite this file https://docs.google.com/document/d/10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y/edit.

I cant file the proper File ID of this. If i am not wrong, this is not the file ID ’10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y’.

Where i can get the real ID of the Google Doc?


Solution

If you are using the value of [10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y] as the file ID, please modify it to 10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y. Ref So, when your script is modified, it becomes as follows.

From:

DriveApp.getFileById("[10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y]").setContent(csvContent)

To:

DriveApp.getFileById("10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y").setContent(csvContent);

Note:

  • If you want to directly use the URL of https://docs.google.com/document/d/###/edit, you can also use the following script.

      DocumentApp.openByUrl("https://docs.google.com/document/d/###/edit").getBody().setText(csvContent);
    

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *