简体   繁体   English

在独立脚本中,通过 id 打开文件

[英]In standalone script, open file by id

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 .所以我设置了一个干净的新未绑定项目并添加了 Service Google Sheets API I referred to the Google Docs and am starting with the "simple" first step, open the Sheet file.我参考了 Google 文档并从“简单”的第一步开始,打开工作表文件。 The unshared URL I'm working on is https://docs.google.com/spreadsheets/d/1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI/edit#gid=1242809349 .我正在处理的未共享 URL 是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使用复制/粘贴的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.异常:在 object SpreadsheetApp 上获取方法或属性 openById 时发生意外错误。

With the Script ID of the file使用文件的Script ID

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

Error message:错误信息:

Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI.异常:服务电子表格在访问 ID 为 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI 的文档时失败。

I believe your goal is as follows.我相信你的目标如下。

  • You want to check whether you have permission to open the file using the file ID.您想检查您是否有权使用文件 ID 打开该文件。
  • You want to achieve this using Google Apps Script.您希望使用 Google Apps 脚本实现此目的。

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.运行此脚本时,如果您没有打开该文件You have permission for opening this file. is shown.显示。 If the file ID is an invalid file ID, Invalid file ID.如果文件 ID 是无效文件 ID,则Invalid file ID. is shown.显示。
  • If you have permission for opening the file, you can use file .如果您有打开文件的权限,则可以使用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.使用DriveApp.getFileById时,可以检查Google Spreadsheet 以外的文件。 So, I used it.所以,我用了它。

Reference:参考:

Added 1:添加 1:

From your following reply,从您的以下回复中,

If I do need to use DriveApp() instead, how do I get the Sheet data from there?如果我确实需要改用 DriveApp(),如何从那里获取工作表数据?

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:添加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.总而言之,到目前为止,我可以使用该 ID 运行您的第一个示例“1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI”并且它运行但我在修改后的示例中遇到错误:异常:服务电子表格在访问 ID 为 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI 的文档时失败。 sample @ sameple_test.gs:19样本@sameple_test.gs:19

From Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI. sample @ sameple_test.gs:19?来自Exception: Service Spreadsheets failed while accessing document with id 1K3Iy7t1TufD9HRJBCqwFbcrfvCvpnqyI. sample @ sameple_test.gs:19? 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) .和文件 ID 的长度,我担心您正在尝试通过SpreadsheetApp.open(file)使用不是电子表格的文件。 If my understanding is correct, such error occurs.如果我的理解是正确的,就会出现这样的错误。 In this case, how about checking the mimeType as follows?在这种情况下,如何检查 mimeType 如下?

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM