I am trying to create a function in the Google Spreadsheet for my coworkers to input the data in the spreadsheet, then click on the "Send" button to achieve the following tasks:
- Manually input data is linked to another sheet (Invoice template), so the apps script is set to generate the other sheet (template) into pdf file
- The pdf file will be sent out automatically using their individual gmail account
- The pdf file will also be automatically stored in a designated folder in Google Drive
This is my first time using Apps Script. I found a set of script from a website and used it for my Spreadsheet. It worked perfectly with my account, but when my coworkers tried to execute it, it showed this error message: "Exception: You do not have permission to access the requested document. at emailInvoiceAsPDF(Invoice:10:29)"
Below is the script that I used for my spreadsheet:
/**
* @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))
}
I tried reading a lot of threads on Google but I have no clue to resolve this problem. I am very frustrated... Please help..
question from:
https://stackoverflow.com/questions/65876786/google-apps-script-error-you-do-not-have-permission-to-access-the-requested-do 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…