在Google表格上创建PDF文件后,通过从对话框中选择“是”来打开/查看PDF文件

以下代码使我可以从电子表格创建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.');
    }; 
    }
rockywell 回答:在Google表格上创建PDF文件后,通过从对话框中选择“是”来打开/查看PDF文件

帮助您解决问题。我创建了一个自定义对话框,在其中构建了HTML模板,然后更改了一些参数,以便能够使用以下功能打开PDF文件:

// Generate a custom HTML to open the PDF
function showPdfFile(pdfFile) {
  // Create the template html
  var htmlString = '<!DOCTYPE html>'
    + '<html> <head> <base target="_top"></head>'
    + '<body>' 
    + '<input type="button" value="Yes" onClick="window.open(##URL##,##TYPE##,width=800,height=600); google.script.host.close();"/>' 
    + '<input type="button" value="No" onclick="google.script.host.close()" />'
    + '</body> </html>';

  // Change the parameters inside the window.open method 
  htmlString = htmlString.replace("##URL##","'" + pdfFile.getUrl() + "'");
  htmlString = htmlString.replace("##TYPE##","'_blank'");

  // Create the output window 
  var html = HtmlService.createHtmlOutput(htmlString)
      .setWidth(400)
      .setHeight(300);

  // Show the Window
  SpreadsheetApp.getUi() 
      .showModalDialog(html,'Do you want to view the PDF file?');
}

您的googlesheetToPDF函数现在看起来像这样:

function googlesheetToPDF(){  

  var id = 'your-ID';  // 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 = 'your-ID'; // Dummy Id
  var folder = DriveApp.getFolderById(folderid);
  var pdfFile = 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);

  // Instead of an if/else statement,call this function 
  showPdfFile(pdfFile);

}

通知

您需要使用CSS为HTML模板中生成的按钮赋予某种样式。

文档

要了解有关自定义对话框的更多信息,请检查以下文档:

本文链接:https://www.f2er.com/3048969.html

大家都在问