简体   繁体   English

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

[英]Open / View a PDF file after creating it on google sheets by selecting “yes” from a dialog box

the following code allows me to create a PDF file from a spreadsheet.以下代码允许我从电子表格创建 PDF 文件。 As you can see it has two dialog boxes.如您所见,它有两个对话框。 The first dialog box is only to notify the user that the file has been created, the second dialog box, in my original code, was to ask if the user wanted to email the PDF file.第一个对话框只是通知用户文件已经创建,第二个对话框,在我的原始代码中,是询问用户是否想要 email PDF 文件。 However on this occasion I do not want the function of sending email, I want to be able to see/view/preview the PDF file.但是在这种情况下,我不希望发送 email 的 function,我希望能够查看/查看/预览 PDF 文件。 I can't make the script show me the PDF file that was just created (view / preview the file).我无法让脚本显示刚刚创建的 PDF 文件(查看/预览文件)。 I hope you can help me since I have not found any help related to this topic.我希望你能帮助我,因为我没有找到与这个主题相关的任何帮助。 Thanks on advance.提前感谢。

    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.');
    }; 
    }

To help you solve your issue.帮助您解决您的问题。 I created a custom Dialog box where I built the HTML template and then changed some parameters to be able to open the PDF file using this function:我创建了一个自定义对话框,在其中构建了 HTML 模板,然后更改了一些参数,以便能够使用此 function 打开 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?');
}

And your googlesheetToPDF function now would look like this:你的googlesheetToPDF function 现在看起来像这样:

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);
    
}

Notice注意

You would need to use CSS to give some style to the buttons generated in the HTML template.您需要使用 CSS 为 HTML 模板中生成的按钮提供一些样式。

Docs文档

To know more about custom Dialog boxes check these Docs:要了解有关自定义对话框的更多信息,请查看以下文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM