异常:文档丢失(可能已被删除,或者您没有读取权限?)

Exception: Document is missing (perhaps it was deleted, or you don't have read access?)

我正在开展一个项目,该项目采用存储在 Google Sheet 中的“配置文件”,为每个配置文件创建一个唯一的 Google 文档,然后更新唯一的 Google 当您按下 Google Sheet.

上的按钮时,记录任何新信息

我的原始代码中内置了一些其他自动化功能,但我将其中的大部分简化为与我遇到的错误相关的内容,即:

Exception: Document is missing (perhaps it was deleted, or you don't have read access?

它发生在我脚本的 fileUpdate 函数的第 52 行。这是供参考的适当行:

  var file = DocumentApp.openById(fileName);

这是我的其余代码:

function manageFiles() {
  //Basic setup. Defining the range and retrieving the spreadsheet to store as an array.
  var date = new Date();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var array = sheet.getDataRange().getValues();
  var arrayL = sheet.getLastRow();
  var arrayW = sheet.getLastColumn();
  for (var i = 1; i < arrayL; i++) {
  if (array[i][arrayW-2] == "") {
//Collect the data from the current sheet.
//Create the document and retrieve some information from it.
    var docTitle = array[i , 0]
      var doc = DocumentApp.create(docTitle);
      var docBody = doc.getBody();
      var docLink = doc.getUrl();
//Use a for function to collect the unique data from each cell in the row.
      docBody.insertParagraph(0 , "Last Updated: "+date);
      for (var j = 2; j <= arrayW; j++) {
        var colName = array[0][arrayW-j];
        var data = array[i][arrayW-j];
        if (colName !== "Filed?") {
          docBody.insertParagraph(0 , colName+": "+data);
        }
      }
//Insert a hyperlink to the file in the cell containing the SID
      sheet.getRange(i+1 , 1).setFormula('=HYPERLINK("'+docLink+'", "'+SID+'")');
//Insert a checkbox and check it.
      sheet.getRange(i+1 , arrayW-1).insertCheckboxes();
      sheet.getRange(i+1 , arrayW-1).setFormula('=TRUE');
  }
else if (array[i][arrayW-2] !== "") {
updateFiles(i);
}
}
sheet.getRange(1 , arrayW).setValue('Last Update: '+date);
}


//Note: I hate how cluttered updateFiles is. I'm going to clean it up later.

function fileUpdate(rowNum) {
//now you do the whole thing over again from createFiles()
//Basic setup. Defining the range and retrieving the spreadsheet to store as an array.
  var date = new Date();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var array = sheet.getDataRange().getValues();
  var arrayL = sheet.getLastRow();
  var arrayW = sheet.getLastColumn();
//Collect the data from the current sheet.
  var fileName = array[rowNum][0];
  var file = DocumentApp.openById(fileName);
//retrieve the body of the document and clear the text, making it blank.
  file.getBody().setText("");
//Use a for function to collect the the unique date from every non-blank cell in the row.
file.getBody().insertParagraph(0 , "Last Updated: "+date);
    for (var j = 2; j <= arrayW; j++) {
      var colName = array[0][arrayW-j];
      var data = array[rowNum][arrayW-j];
      file.getBody().insertParagraph(0 , colName+": "+data);
      }
  }

如果您想查看我的示例电子表格,可以查看 here。不过,我建议您复制一份,因为您无权访问我的脚本创建的 Google 文档。

我查看了其他一些出现同样错误的论坛并尝试了几个规定的解决方案(注销其他 Google 帐户,清除我的 cookie,用反斜杠完成 URL , 使用 link) 扩大对所有人的权限,但无济于事。

**任何被我的简陋代码或格式冒犯的人请注意:我是自学成才的,所以如果我的作品难以阅读,我深表歉意。

问题(在附加到您的 sheet 的更新代码中)来自您的 URL

Side Note:

In your initial question, you define DocumentApp.openById(fileName); I assume your realized that this is not correct, since you updated your code to DocumentApp.openByUrl(docURL);, so I will discuss the problem of the latter in the following.

您 sheet 中的 URL 形式为

https://docs.google.com/open?id=1pT5kr7V11TMH0pJea281VhZg_1bOt8YDRrh9thrUV0w

DocumentApp.openByUrl 期望 URL 形式

https://docs.google.com/document/d/1pT5kr7V11TMH0pJea281VhZg_1bOt8YDRrh9thrUV0w/

仅仅添加一个/是不够的!

要么手动创建预期的 URL,要么 - 更容易/改用方法 DocumentApp.openById(id)

为此,您可以从 URL 中提取 id,如下所示:

var id = docURL.split("https://docs.google.com/open?id=")[1];
var file = DocumentApp.openById(id)