谷歌企业应用脚本错误:"You do not have permission to access the requested document"



我正在尝试在Google电子表格中创建一个函数,以便我的同事在电子表格中输入数据,然后单击"发送"。按钮,实现以下任务:

  1. 手动输入数据链接到另一个表(发票模板),所以应用程序脚本被设置为生成另一个表(模板)成pdf文件
  2. pdf文件将自动发送到他们的个人gmail帐户
  3. pdf文件也将自动存储在指定的文件夹在Google Drive

这是我第一次使用Apps Script。我从一个网站上找到了一套脚本,并用它制作了我的电子表格。它在我的帐户上工作得很好,但是当我的同事试图执行它时,它显示了这个错误消息:"Exception: You do not have permission to access the requested document. at emailInvoiceAsPDF(Invoice:10:29)"

下面是我的电子表格脚本:

/**
* @NotOnlyCurrentDoc
*/
function emailInvoiceAsPDF() {
DocumentApp.getActiveDocument();
DriveApp.getFiles();
/**
* @NotOnlyCurrentDoc
*/
// This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
// Add the link to your spreadsheet here 
// or you can just replace the text in the link between "d/" and "/edit"
const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1pzKLOSnKt2NNCOL8-mG1YVgELp8Hf-v9PhMkSI-YYte/edit");
// We are going to get the email address from the cell "B7" from the "Invoice" sheet
// Change the reference of the cell or the name of the sheet if it is different
const value = ss.getSheetByName("Invoice Template").getRange("J32").getValue();
const email = value.toString();
var sheet1=ss.getSheetByName('Invoice template');
var subject = sheet1.getRange("B2:N2").getValue();
// Subject of the email message

// Email Text. You can add HTML code here - see ctrlq.org/html-mail
const body = "Dear customer,<br><br> Please find the attached Invoice. <br><br> Thank You.";
// Again, the URL to your spreadsheet but now with "/export" at the end
// Change it to the link of your spreadsheet, but leave the "/export"
const url = 'https://docs.google.com/spreadsheets/d/1pzSZZSnKtPPieRL8-mG1YVgELp7Jv-v9PhMkSI-AeZA/export?';
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&portrait=true' + // orientation portal, use false for landscape
'&scale=4' +
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid=196383625'; // the sheet's Id. Change it to your sheet ID.
// You can find the sheet ID in the link bar. 
// Select the sheet that you want to print and check the link,
// the gid number of the sheet is on the end of your link.

var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

// Generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();

// Send the PDF file as an attachement 
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: "Invoice" + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
// Save the PDF to Drive. The name of the PDF is going to be the name of the Company (cell B5)
const nameFile = ss.getSheetByName("Invoice Template").getRange("B2").getValue().toString() +".pdf"
var Folder = DriveApp.getFolderById("12oHonHMo0ZBS-qZkxe1Iuyfm5miy0fXQ");
Folder.createFile(response.setName(nameFile))
}

我试着在谷歌上阅读很多线程,但我没有线索来解决这个问题。我很沮丧……请帮. .

允许用户访问脚本中使用的所有文件和文件夹不会抛出错误。

如果用户没有编辑器

当用户访问要存储PDF的文件夹或脚本中涉及的其他文件夹/文件时,将发生此错误。

相关内容

最新更新