【问题标题】:Google Sheets Export to PDF Script - Why is it exporting hidden rows?Google Sheets Export to PDF Script - 为什么要导出隐藏的行?
【发布时间】:2020-11-18 18:59:38
【问题描述】:

我有一个脚本,用于将单个 google 表格保存为给定文件夹中的 pdf 文档。

由于工作表的内容是动态的,并且有时底部会加载一些空网格行,因此我提供了一种在导出文件之前隐藏没有内容的行的方法。

直到最近,这一直按要求工作。现在该函数隐藏了行,但创建的文件将这些行显示为未隐藏。

知道这是否是由于最近更改了 google sheet api 造成的,我可以做些什么来恢复旧功能吗?

这是我以前工作的代码块:

// API function for saving a single sheet without hiding sheets
// *******************************************************************************


function singleSheetAPIExport(){

  // Get active spreadsheet URL
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = 'Example';
  var main = ss.getSheetByName(sheetName)
  var folderID = '1wsxxxxxxblahblah' // Google Drive Folder ID

  Logger.log('maxrows: ' + main.getMaxRows());
  Logger.log('last row: ' + main.getLastRow());

  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                                   // paper size legal / letter / A4
  + '&portrait=true'                                // orientation, false for landscape
  + '&fitw=true&source=labnol'                       // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false'             // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false'             // hide page numbers and gridlines
  + '&fzr=false'                                     // do not repeat row headers (frozen rows) on each page
  + '&gid=';                                         // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  // Hide All Empty Rows in the Print Sheet
  var maxRows = main.getMaxRows(); 
  var lastRow = main.getLastRow();
  if (maxRows-lastRow != 0){
    main.hideRows(lastRow+1, maxRows-lastRow);
  }

  // Get the response for your specific sheet that can later be converted to blob
  var response = UrlFetchApp.fetch(url + url_ext + main.getSheetId(), {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  // Save pdf version
  var saveFolder = 'PDF';
  var parentFolder = DriveApp.getFolderById(folderID);
  var folder, folders = DriveApp.getFoldersByName(saveFolder);
   if (folders.hasNext()) {
     folder = folders.next();
   } else {
     folder = parentFolder.createFolder(saveFolder);
   }
  var name = main.getRange("B8").getValue(); 
  var cleanName = name.replace(/([^a-zA-Z0-9() #%~-])/g, "-");
  folder.createFile(response.getBlob().setName(cleanName));

  // Unhide the rows again
  var fullSheetRange = main.getRange(1,1,main.getMaxRows(), main.getMaxColumns());
  main.unhideRow(fullSheetRange); 

}

功能在 6 月 17 日至 19 日之间发生了变化,这可能表明 api 发生了变化,但我找不到任何证据表明这种变化。

我之前在 Stack Exchange 的 Web 应用程序站点上发布了此内容,但由于我看到 google 应用程序团队推荐 Stackoverflow 进行查询,因此已将其删除并重新发布。

【问题讨论】:

  • 能否请您提供最新的表格(隐藏不再起作用的地方)?
  • 不确定如何将表格复制到我的问题中,但这里是示例 google 表格文档docs.google.com/spreadsheets/d/…

标签: pdf google-apps-script google-sheets


【解决方案1】:

我在您提供的测试表上测试了您的代码,它按预期工作。但是,如果我创建大量额外的行 - 实际上我可以重现您的隐藏行被导出的错误。

您的代码中的问题是您在隐藏空行后立即创建文件并在创建文件后立即取消隐藏行:

您必须知道createFile() 是一个异步请求 - 就像所有 Google API 调用一样。这意味着如果文件较大,创建可能需要更长的时间,您的 unhideRow() 方法可能会在创建文件之前执行。

类似地,如果您在隐藏所有行之前执行调用,这将导致您出现同样的问题。解决方案在于使用SpreadsheetApp.flush(); 函数,该函数确保仅在前一个函数完成后才执行函数。在folder.createFile(response.getBlob().setName(cleanName)); 之前和之后插入SpreadsheetApp.flush(); 可以解决您的问题。

【讨论】:

  • 太好了 - 感谢您帮助我。奇怪的是它工作了这么久,最近才开始出现问题,它让我完全错误的调试轨道!
  • 我遇到了类似的问题,我使用 saveAndClose() 在导出之前将更改刷新到 Google Doc。 developers.google.com/apps-script/reference/document/…
【解决方案2】:

还有另一种将工作表导出为 PDF 文件的方法,它指明了要导出的所需单元格。而且您不需要隐藏不必要的行。

function exportPDF(ssID,source,options,format){
  var dt=new Date();
  var d=encodeDate(dt.getFullYear(),dt.getMonth(),dt.getDate(),dt.getHours(),dt.getMinutes(),dt.getSeconds());
  var pc=[null,null,null,null,null,null,null,null,null,0,
          source,
          10000000,null,null,null,null,null,null,null,null,null,null,null,null,null,null,
          d,
          null,null,
          options,
          format,
          null,0,null,0];
  
 var js = " \
    <script> \
      window.open('https://docs.google.com/spreadsheets/d/"+ssID+"/pdf?esid="+(Math.round(Math.random()*10000000))+"&id="+ssID+"&a=true&pc="+JSON.stringify(pc)+"&gf=[]'); \
      google.script.host.close(); \
    </script> \
  ";
  
  var html = HtmlService.createHtmlOutput(js)
    .setHeight(10)
    .setWidth(100);
  SpreadsheetApp.getUi().showModalDialog(html, "Save To PDF"); 
}
function myExportPDF(){
  var as = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = as.getActiveSheet();
  exportPDF(as.getId(), // Идентификатор таблицы
    [
      [sheet.getSheetId().toString(), // Sheet ID
       0,                             // vertical start border (from the first line)
       160,                           // vertical end boundary (160th line inclusive)
       0,                             // horizontal start border (from cell A)
       8                              // horizontal end boundary (cell H inclusive)
      ]
    ],
    [
      0,         // Do not show notes
      null,
      1,         // Show grid lines
      0,         // Do not show page numbers
      0,         // Do not show book title
      0,         // Do not show sheet title
      0,         // Do not show date
      0,         // Do not show time
      1,         // Repeat pinned rows
      1,         // Repeat pinned columns
      1,         // Page order down then up
      1,
      null,
      null,
      1,         // Left Alignment
      1          // Top Alignment
    ],
    [
      "A4",      // A4 sheet format
      1,         // Page Orientation Vertical
      2,         // Align to height
      1,
      [
        0.75,    // Top margin 0.75 inch
        0.75,    // Bottom margin 0.75 inch
        0.7,     // Left margin 0.7 inch
        0.7      // Right margin 0.7 inch
      ]
    ]
  );
}

详细说明在我的网站上 https://kandiral.ru/googlescript/eksport_tablic_google_sheets_v_pdf_fajl.html

UPD:上面的代码用于下载 PDF 文件。这是一个保存到 Google Drive 的脚本

function exportPDFtoGDrive(ssID,filename,source,options,format){
  var dt=new Date();
  var d=encodeDate(dt.getFullYear(),dt.getMonth(),dt.getDate(),dt.getHours(),dt.getMinutes(),dt.getSeconds());
  var pc=[null,null,null,null,null,null,null,null,null,0,
          source,
          10000000,null,null,null,null,null,null,null,null,null,null,null,null,null,null,
          d,
          null,null,
          options,
          format,
          null,0,null,0];
  
  var folder=null;
  var parents = DriveApp.getFileById(ssID).getParents();
  if (parents.hasNext())folder = parents.next();
  else folder = DriveApp.getRootFolder();
  
  var options = {
    'method': 'post',
    'payload': "a=true&pc="+JSON.stringify(pc)+"&gf=[]",
    'headers': {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
    'muteHttpExceptions': true
  };
  var theBlob = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/"+ssID+"/pdf?id="+ssID, options).getBlob();
  folder.createFile(theBlob).setName(filename+".pdf");
}

function myExportPDFtoGDrive(){
  var as = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = as.getActiveSheet();
  var filename = as.getName()+" "+sheet.getName();
  exportPDFtoGDrive(
    as.getId(),  // Идентификатор таблицы
    filename,    // Имя файла
    [[sheet.getSheetId().toString(),0,160,0,8]],
    [0,null,1,0,0,0,0,0,1,1,1,1,null,null,1,1],
    ["A4",1,2,1,[0.75,0.75,0.7,0.7]]
  );
}

UPD:为下载请求添加了 esid 变量

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2010-12-02
    • 2014-08-28
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多