尝试将我的 google sheet 以 pdf 格式保存到我的驱动器文件夹时出现问题
Problem trying to save my google sheet as a pdf to my drive folder
- 当我从我的 gmail acc
中使用脚本时,脚本可以正常工作
- 但是当我将它分享到另一个 gmail ACC 时,脚本总是出错!
- 错误
Exception: Access denied: DriveApp.
我使用此代码将特定 sheet 以 pdf 格式保存到我的驱动器,并将其邮寄给特定的人。
当其他用户被授予使用此 sheet 的权限时,我希望代码正常 运行。请帮我解决这个问题。
function sendReport(range) {
SpreadsheetApp.getActive().getSheetByName("Customers").hideSheet();
SpreadsheetApp.getActive().getSheetByName("Products").hideSheet();
SpreadsheetApp.getActive().getSheetByName("SO Log").hideSheet();
SpreadsheetApp.getActive().getSheetByName("SOF").hideSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SOSheet = ss.getSheetByName("Sales Order");
var name = SOSheet.getRange('F10').getValue();
var message = {
to: "random@gmail.com",
subject: "Sales Order",
body: "Hi team,\n\nPlease find the Sales Order attached.\n\nThank you",
name: "Dave",
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("SO"+name)]
}
MailApp.sendEmail(message);
SpreadsheetApp.getActive().getSheetByName("Customers").showSheet();
SpreadsheetApp.getActive().getSheetByName("Products").showSheet();
SpreadsheetApp.getActive().getSheetByName("SO Log").showSheet();
SpreadsheetApp.getActive().getSheetByName("SOF").showSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var token = ScriptApp.getOAuthToken();
var sheet = ss.getSheetByName("Sales Order");
var bogus = DriveApp.getRootFolder();
//Creating an exportable URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/exSOrt?".replace("SS_ID", ss.getId());
var folderID = "1KU7ylGsci9qsVzxU2ZlCW"; // Folder id to save in a folder.
var folder = DriveApp.getFolderById(folderID);
var SOcode = ss.getRange("'Sales Order'!F10").getValue()
var pdfName = "SO" + SOcode;
/* Specify PDF exSOrt parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext = 'exportFormat=pdf&format=pdf' // exSOrt as pdf / csv / xls / xlsx
+ '&size=A4' // paper size legal / letter / A4
+ '&SOrtrait=true' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&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='; // the sheet's Id
// Convert individual worksheet to PDF
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},muteHttpExceptions:true});
;
var blobs = response.getBlob().setName(pdfName + '.pdf');
var folders = folder.getFoldersByName(pdfName);
folder = folders.hasNext() ? folders.next() : folder.createFolder(pdfName);
var newFile = folder.createFile(blobs);
var newFileLink = newFile.getUrl();
var SOcode = ss.getRange("'SOF'!F12").getValue();
var writePDFLink = ss.getRange("'SOF'!L1").setValue(newFileLink);
// Define the scope
Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
}
方法 createFolder(name) 需要 Editor
权限
A Viewer
将收到 Exception: Access denied: DriveApp
错误。
因此,您需要以编辑权限共享文件:
- 当我从我的 gmail acc 中使用脚本时,脚本可以正常工作
- 但是当我将它分享到另一个 gmail ACC 时,脚本总是出错!
- 错误
Exception: Access denied: DriveApp.
我使用此代码将特定 sheet 以 pdf 格式保存到我的驱动器,并将其邮寄给特定的人。 当其他用户被授予使用此 sheet 的权限时,我希望代码正常 运行。请帮我解决这个问题。
function sendReport(range) {
SpreadsheetApp.getActive().getSheetByName("Customers").hideSheet();
SpreadsheetApp.getActive().getSheetByName("Products").hideSheet();
SpreadsheetApp.getActive().getSheetByName("SO Log").hideSheet();
SpreadsheetApp.getActive().getSheetByName("SOF").hideSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SOSheet = ss.getSheetByName("Sales Order");
var name = SOSheet.getRange('F10').getValue();
var message = {
to: "random@gmail.com",
subject: "Sales Order",
body: "Hi team,\n\nPlease find the Sales Order attached.\n\nThank you",
name: "Dave",
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("SO"+name)]
}
MailApp.sendEmail(message);
SpreadsheetApp.getActive().getSheetByName("Customers").showSheet();
SpreadsheetApp.getActive().getSheetByName("Products").showSheet();
SpreadsheetApp.getActive().getSheetByName("SO Log").showSheet();
SpreadsheetApp.getActive().getSheetByName("SOF").showSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var token = ScriptApp.getOAuthToken();
var sheet = ss.getSheetByName("Sales Order");
var bogus = DriveApp.getRootFolder();
//Creating an exportable URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/exSOrt?".replace("SS_ID", ss.getId());
var folderID = "1KU7ylGsci9qsVzxU2ZlCW"; // Folder id to save in a folder.
var folder = DriveApp.getFolderById(folderID);
var SOcode = ss.getRange("'Sales Order'!F10").getValue()
var pdfName = "SO" + SOcode;
/* Specify PDF exSOrt parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext = 'exportFormat=pdf&format=pdf' // exSOrt as pdf / csv / xls / xlsx
+ '&size=A4' // paper size legal / letter / A4
+ '&SOrtrait=true' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&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='; // the sheet's Id
// Convert individual worksheet to PDF
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},muteHttpExceptions:true});
;
var blobs = response.getBlob().setName(pdfName + '.pdf');
var folders = folder.getFoldersByName(pdfName);
folder = folders.hasNext() ? folders.next() : folder.createFolder(pdfName);
var newFile = folder.createFile(blobs);
var newFileLink = newFile.getUrl();
var SOcode = ss.getRange("'SOF'!F12").getValue();
var writePDFLink = ss.getRange("'SOF'!L1").setValue(newFileLink);
// Define the scope
Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
}
方法 createFolder(name) 需要 Editor
权限
A
Viewer
将收到Exception: Access denied: DriveApp
错误。因此,您需要以编辑权限共享文件: