Home > Back-end >  In standalone script, open file by id
In standalone script, open file by id

Time:01-27

My current goal is a standalone script that I can run on multiple Sheets files in my Drive.

So I've setup a clean new unbound Project and added the Service Google Sheets API. I referred to the Google Docs and am starting with the "simple" first step, open the Sheet file. The unshared URL I'm working on is https://docs.google.com/spreadsheets/d/1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI/edit#gid=1242809349. I should clarify that it's shared with my account.

My question is how do I interpret these errors and address them in order to open the file. Is there any way to use a better debugger?

With a copy/pasted gid

function myFunction() {
  var ss = SpreadsheetApp.openById("1242809349");
  Logger.log(ss.getName());
}

Error message:

Exception: Unexpected error while getting the method or property openById on object SpreadsheetApp.

With the Script ID of the file

function myFunction() {
  var ss = SpreadsheetApp.openById("1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI");
  Logger.log(ss.getName());
}

Error message:

Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI.

CodePudding user response:

I believe your goal is as follows.

  • You want to check whether you have permission to open the file using the file ID.
  • You want to achieve this using Google Apps Script.

In this case, how about the following sample script?

Sample script:

function sample() {
  const fileId = "###"; // Please set your file ID.
  
  let file;
  try {
    file = DriveApp.getFileById(fileId);

    // If you have permission for opening the file, you can use `file`.
    const owner = file.getOwner().getEmail();
    const editors = file.getEditors().map(e => e.getEmail());
    const viewers = file.getViewers().map(e => e.getEmail());
    console.log(owner);
    console.log(editors);
    console.log(viewers);

  } catch ({message}) {
    if (message == "No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it.") {
      console.log("You have the permission for opening this file.");
    } else if (message == "Unexpected error while getting the method or property getFileById on object DriveApp.") {
      console.log("Invalid file ID.");
    } else {
      console.log(message);
    }
  }
}
  • When this script is run, if you don't have permission for opening the file, You have permission for opening this file. is shown. If the file ID is an invalid file ID, Invalid file ID. is shown.
  • If you have permission for opening the file, you can use file. In this sample, the owner, editors, and viewers are shown.
  • When DriveApp.getFileById is used, the files except for Google Spreadsheet can be checked. So, I used it.

Reference:

Added 1:

From your following reply,

If I do need to use DriveApp() instead, how do I get the Sheet data from there?

Sample script:

function sample() {
  const fileId = "###"; // Please set your file ID.
  
  let file;
  try {
    file = DriveApp.getFileById(fileId);
  } catch ({message}) {
    if (message == "No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it.") {
      console.log("You have the permission for opening this file.");
    } else if (message == "Unexpected error while getting the method or property getFileById on object DriveApp.") {
      console.log("Invalid file ID.");
    } else {
      console.log(message);
    }
    return;
  }

  // You can use `file` by Spreadsheet as follows.
  const spreadsheet = SpreadsheetApp.open(file);
}

Added 2:

From your following reply,

To summarize so far, I can run your first example with that id "1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI" and it runs but I get an error with the revised example: Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI. sample @ sameple_test.gs:19

From Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI. sample @ sameple_test.gs:19? and the length of the file ID, I'm worried that you are trying to use the file, which is not Spreadsheet, by SpreadsheetApp.open(file). If my understanding is correct, such error occurs. In this case, how about checking the mimeType as follows?

Sample script:

function sample() {
  const fileId = "###"; // Please set your file ID.

  let file;
  try {
    file = DriveApp.getFileById(fileId);
    if (file.getMimeType() != MimeType.GOOGLE_SHEETS) {
      console.log("This file is not Spreadsheet.")
      return;
    }
  } catch ({ message }) {
    if (message == "No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it.") {
      console.log("You have the permission for opening this file.");
    } else if (message == "Unexpected error while getting the method or property getFileById on object DriveApp.") {
      console.log("Invalid file ID.");
    } else {
      console.log(message);
    }
    return;
  }

  // You can use `file` by Spreadsheet as follows.
  const spreadsheet = SpreadsheetApp.open(file);
}
  • Related