以下代码使我可以从电子表格创建PDF文件。如您所见,它有两个对话框。第一个对话框仅用于通知用户文件已创建,第二个对话框用我的原始代码询问用户是否要通过电子邮件发送PDF文件。但是,在这种情况下,我不需要发送电子邮件的功能,而是希望能够查看/查看/预览PDF文件。我无法使脚本向我显示刚刚创建的PDF文件(查看/预览文件)。我没有找到与该主题相关的任何帮助,希望您能为我提供帮助。提前谢谢。
function googlesheetToPDF(){
var id = '1C8DZEQwSv6wjq6kr1TSWldUuiThWsjXCT6d6aghmkPu'; // Dummy Id
var index = 0;
var ss = Spreadsheetapp.getactiveSpreadsheet();
var hoja = ss.getactiveSheet();
var columnas = hoja.getRange("M1:P1");
hoja.hideColumn(columnas);
var Hojaactiva = ss.getSheetByName('Hoja 1');
// variables to construct the name of the file
var fecha = Utilities.formatDate(new Date(),"GMT-6","dd/MM/yy HH:mm");
var cotizacionNo = Hojaactiva.getRange("I2").getDisplayValue();
var cliente = Hojaactiva.getRange("F8").getvalue();
var correo = Hojaactiva.getRange("F10").getvalue();
var nombre = cotizacionNo +' - ' + cliente + ' - ' + fecha + '.pdf'; // Name of the PDF
Spreadsheetapp.flush();
var theurl = 'https://docs.google.com/spreadsheets/d/'
+ id
+ '/export?exportFormat=pdf&format=pdf'
+ '&size=letter'
+ '&portrait=true'
+ '&fitw=true'
+ '&sheetnames=false&printtitle=false&pagenumbers=false'
+ '&gridlines=false'
+ '&fzr=true'
+ '&gid=
+ index;
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl,{ headers: { 'Authorization': 'Bearer ' + token } });
var pdf = docurl.getBlob().setName(nombre).getas('application/pdf');
var folderid = '14DmJKycu_ysc0ewkWGzvItOFdMx47i4b'; // Dummy Id
var folder = DriveApp.getFolderById(folderid);
folder.createFile(pdf);
var hoja = ss.getactiveSheet();
var columnas = hoja.getRange("M1:P1");
hoja.unhideColumn(columnas);
var archivo = docurl.getBlob().getas('application/pdf').getBytes();
var attach = {fileName:nombre,content:archivo,mimeType:'application/pdf'};
Spreadsheetapp.getUi().alert('New document created in' + ' ' + folder);
var ui = Spreadsheetapp.getUi();
var response = ui.alert('Do you want to view the PDF file?',ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
//Here is where I´m stuck
} else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
};
}