简体   繁体   English

在 Apps 脚本中,导出 pdf 时在哪里以及如何添加页面格式详细信息?

[英]In Apps Script, where and how to add page format detail when exporting a pdf?

New to apps scripts so this is probably a very basic question, more familiar with visual basic from a long time ago.应用程序脚本的新手,所以这可能是一个非常基本的问题,很久以前就对 Visual Basic 更加熟悉。 I have put together a script that does everything I want with one exception.我整理了一个脚本,除了一个例外,它可以完成我想要的一切。 It exports a PDF from a google sheet to the appropriate google drive folder I have created and also emails me a copy.它将 PDF 从 google 工作表导出到我创建的相应 google 驱动器文件夹,并通过电子邮件发送给我一份副本。 Only thing is the formatting of the exported PDF. It is off center and I don't know where in the script to add the relevant margin control, fit to page, portrait vs landscape.唯一的问题是导出的 PDF 的格式。它偏离中心,我不知道在脚本中的哪个位置添加相关的边距控制、适合页面、纵向与横向。

I looked at various examples of PDF export scripts that had page set up control in them and tried to incorporate them into my existing script, but not being familiar enough with the structure, I wasn't getting proper results and was often generating an error in the script running properly at all.我查看了 PDF 导出脚本的各种示例,其中包含页面设置控件,并尝试将它们合并到我现有的脚本中,但由于对结构不够熟悉,我没有得到正确的结果并且经常在脚本运行正常。 So I removed all of those attempts and restored the code that was functioning (just without the exported PDF looking how I want).所以我删除了所有这些尝试并恢复了正在运行的代码(只是没有导出 PDF 看起来像我想要的那样)。 I will post the current code here - hope this is the appropriate place - just signed up for this site.我将在这里发布当前代码 - 希望这是合适的地方 - 刚刚注册了这个网站。 And thanks in advance for any help.并提前感谢您的帮助。

function exportSheet() { 

  var sheetName2 = 'PDF for Export'   
  var ss = SpreadsheetApp.openById('1vYR5vtHkIkVDZtbSxsocOYEF1DNkaOPnc47irOpE_Rc')
  var namen = SpreadsheetApp.openById('1vYR5vtHkIkVDZtbSxsocOYEF1DNkaOPnc47irOpE_Rc').getRange("PDF Builder!E2").getValues();
  var number = SpreadsheetApp.openById('1vYR5vtHkIkVDZtbSxsocOYEF1DNkaOPnc47irOpE_Rc').getRange("PDF Builder!E1").getValues();
  var nextnumber = +number + 1
  var sheets = ss.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].getSheetName() !== sheetName2) {
      sheets[i].hideSheet()
    }
  }
  var newFile = DriveApp.createFile(ss.getBlob());
  for (var i = 0; i < sheets.length; i++) {
    sheets[i].showSheet()
  }
  newFile.setName(namen);
  newFile.moveTo(DriveApp.getFolderById('1I8whlpP1f56SufXEp5eKBTLzIBnA6Fau')); 
  GmailApp.sendEmail('daryn@cineviewstudiosfl.com','NEW QUOTE','NEW QUOTE',
  {attachments: [newFile]});

  SpreadsheetApp.getActiveSheet().getRange('PDF Builder!E1').setValue(nextnumber);

}

Try this modified code and experiment on the PDF parameters.试试这个修改后的代码并在 PDF 参数上进行实验。 One note: for privacy reasons, no need to include the ID of your doc or gDrive folder when you share your code.注意事项:出于隐私原因,在共享代码时无需包含文档或 gDrive 文件夹的 ID。

Hope this helps.希望这可以帮助。

function exportSheet() { 

  var sheetName2 = 'PDF for Export'   
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var namen = ss.getRange("PDF Builder!E2").getValues();
  var number = ss.getRange("PDF Builder!E1").getValues();
  var nextnumber = +number + 1
  var sheets = ss.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].getSheetName() !== sheetName2) {
      sheets[i].hideSheet()
    }
  }

  var PDF4Export = ss.getSheetByName(sheetName2);
  
  // Base URL of your export file
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
  
  // Specify PDF export parameters
    var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
         + '&size=A4' // paper size legal / letter / A4
         + '&portrait=true' // orientation, false for landscape
         + '&fitw=true' // fit to page width, false for actual size
         + '&top_margin=0.25'              
         + '&bottom_margin=0.25'          
         + '&left_margin=0.25'             
         + '&right_margin=0.25' 
         + '&sheetnames=false&printtitle=false' // hide optional headers and footers
         + '&pagenumbers=false&gridlines=true' // hide page numbers and gridlines
         + '&fzr=false' // do not repeat row headers (frozen rows) on each page
         + '&gid='; //the ID of the sheet to be exported is appended here. See response variable

    var token = ScriptApp.getOAuthToken();
    var response = UrlFetchApp.fetch(url + url_ext + PDF4Export.getSheetId(), {
            headers : {
                'Authorization' : 'Bearer ' + token
            },
            muteHttpExceptions: true
        });
  
  //just get the exported file, not the entire worksheet
  var newFile = DriveApp.createFile(response.getBlob());
  for (var i = 0; i < sheets.length; i++) {
    sheets[i].showSheet()
  }
  newFile.setName(namen);
  newFile.moveTo(DriveApp.getFolderById('your_gDrive_folder_id_here')); 
  GmailApp.sendEmail('john.doe@gmail.com','TEST','TEST',
  {attachments: [newFile]});

  SpreadsheetApp.getActiveSheet().getRange('PDF Builder!E1').setValue(nextnumber);

}

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

相关问题 使用 Google Apps 脚本从 Google 表格导出 PDF 时的 setName - setName when exporting a PDF from Google Sheets with Google Apps Script 谷歌表格应用程序脚本,如何格式化保存到谷歌驱动器的 pdf? - Google sheets apps script, how to format a pdf saved to google drive? 通过 Apps Script 仅将特定范围导出为 pdf 有哪些参数? - What parameters are there for exporting only a specific range as pdf through Apps Script? 使用 Google Apps 脚本中的自定义页眉或页脚将 Google 表格导出为 PDF - Exporting Google Sheet as PDF with Custom Headers or Footers in Google Apps Script 使用Google Apps脚本将Google电子表格导出为RSS格式 - Exporting Google spreadsheet to RSS format using Google Apps Script Google 应用脚本 PDF 适合页面 - Google apps script PDF fit to page 在 Apps 脚本中将 Google 表格转换为 PDF 格式时文件大小增加 - File size increases when converting a Google Sheet into PDF format in Apps script 如何使用Google Apps脚本在文档中添加页码 - How to add page number in a document using Google Apps Script 如何使用 Google Apps Script 将 PDF 正文格式化为看起来像电子邮件? - How to format PDF body to look like the email using Google Apps Script? 在Google Apps脚本中将工作表导出为XLS时发生OAuth错误 - OAuth error when exporting Sheet as XLS in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM