我刚刚开始编写脚本,我正在尝试通过更改单元格值(示例A1,我尝试但我没有获得任何内容)或每8小时将Google电子表格(多张)转换为PDF文件。下面的脚本有效,但它只在我运行脚本时创建PDF。我试图添加项目的触发器:基于时间每8小时但我收到一封电子邮件:错误的值(第4行,文件“代码”)所以在文件夹中没有新的pdf文件。
function savePDFs( optSSId, optSheetId ) {
// If a sheet ID was provided, open that sheet, otherwise assume script is
// sheet-bound, and open the active spreadsheet.
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) :
SpreadsheetApp.getActiveSpreadsheet();
// Get folder containing spreadsheet, for later export
var parents = DriveApp.getFileById(ss.getId()).getParents();
if (parents.hasNext()) {
var folder = parents.next();
}
else {
folder = DriveApp.getRootFolder();
}
//additional parameters for exporting the sheet as a pdf
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
// Print either the entire Spreadsheet or the specified sheet if
optSheetId is provided
+ (optSheetId ? ('&gid=' + sheet.getSheetId()) : ('&id=' + ss.getId()))
// following parameters are optional...
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional
headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each
page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
var date = Utilities.formatDate8new Date(). "GMT+2" ""hh:mm dd/MM/yyyy")
var response = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/" +
url_ext, options);
var blob = response.getBlob().setName(ss.getName() + date + '.pdf');
//from here you should be able to use and manipulate the blob to send and
email or create a file per usual.
//In this example, I save the pdf to drive
folder.createFile(blob);
}
当我尝试运行脚本时自动更改单元格值我写了下面报告的另一个脚本(不工作):
function onEdit(e) {
var langName = 'Sheet1'
var langCell = 'A1'
var curSheet = e.range.getSheet()
if (curSheet.getName() === langName) {
if (e.range.getA1Notation() === langCell) {
savePDFs()
}
}