通过从对话框中选择 "yes" 在 google 页上创建 PDF 文件后打开/查看该文件

Open / View a PDF file after creating it on google sheets by selecting "yes" from a dialog box

以下代码允许我从电子表格创建 PDF 文件。如您所见,它有两个对话框。第一个对话框只是通知用户文件已创建,第二个对话框在我的原始代码中是询问用户是否要通过电子邮件发送 PDF 文件。不过这次我不想要发送邮件的功能,我希望能够see/view/preview 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.');
    }; 
    }

帮助您解决问题。我创建了一个自定义对话框,我在其中构建了 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 模板中生成的按钮添加一些样式。

文档

要了解有关自定义对话框的更多信息,请查看这些文档: