目前,我的脚本使用名称 export.pdf
保存 PDF,然后将其重命名为我在电子表格的一个单元格中拥有的值,问题是也就是说,我的网站的 API 最终会在创建文件时获取该文件,并且所有内容都像 export.pdf
一样收集,而不是我真正需要的名称。
是否可以选择使用正确的名称创建PDF而不是重命名它?
//Create PDF
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'IDSPREADSHEETIDSPREADSHEETIDSPREADSHEET' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'IDPAGEIDPAGEIDPAGEIDPAGE';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
//...get token and Blob (do not create the file);
var fileName = ss.getSheetByName("Gerais").getRange("H2").getValue();
//Access or create the 'Squads' folder;
var folder;
var folders = DriveApp.getFoldersByName("Squads");
if(folders.hasNext()) {
folder = folders.next();
}else {
folder = DriveApp.createFolder("Squads");
}
//Remove duplicate file with the same name;
var existing = folder.getFilesByName(fileName);
if(existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
var dres = UrlFetchApp.fetch(durl,{
method: 'delete',
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer '+token}
});
var status = dres.getResponseCode();
if (status >=400) {
} else if (status == 204) {
folder.createFile(pdfBlob).setName(fileName);
}
}
} else {
folder.createFile(pdfBlob).setName(fileName);
最佳答案
问题:
创建文件后设置文件名
folder.createFile(pdfBlob).setName(fileName);
解决方案:
Set the blob name创建文件之前:
folder.createFile(pdfBlob.setName(fileName));
关于pdf - 使用名称创建 PDF,而不是在驱动器中创建文件并重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64067641/