使用名称创建 PDF,而不是在驱动器中创建文件并重命名
Create PDF with name instead of creating the file in drive and renaming
目前,我的脚本使用名称 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));
目前,我的脚本使用名称 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));