导出为PDF时更改文档方向 [英] Change document orientation when exporting to PDF

查看:105
本文介绍了导出为PDF时更改文档方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,可以定期通过电子邮件将一张表的内容发送给所有协作者.

I have this script that emails the content of a speadsheet to all the collaborators on a regular basis.

function myFunction() {
  var document = SpreadsheetApp.openById("123documentid456");

  var editors = document.getEditors();
  for(var i = 0; i < editors.length; i++){
    MailApp.sendEmail(editors[i].getEmail(), "Subject", "Some message", {
      attachments : [document.getAs(MimeType.PDF)]
    });
  }
}

它将创建PDF并通过电子邮件发送.问题是由于PDF的方向是纵向,因此内容不能很好地显示.有什么办法可以将其导出到景观中吗?

It creates a PDF and emails it. The thing is the content does not display nicely as the PDF's orientation is portrait. Is there any way to make it export to landscape?

推荐答案

来自

From here, this code could help

*************************************************

function savePDFs() {
  SpreadsheetApp.flush();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  var url = ss.getUrl();

  //remove the trailing 'edit' from the url
  url = url.replace(/edit$/,'');

  //additional parameters for exporting the sheet as a pdf
  var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf
  //below parameters are optional...
  '&size=letter' + //paper size
  '&portrait=false' + //orientation, false for landscape, true for portrait
  '&fitw=true' + //fit to width, false for actual size
  '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
  '&gridlines=false' + //hide gridlines
  '&fzr=false' + //do not repeat row headers (frozen rows) on each page
  '&gid=' + sheet.getSheetId(); //the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url + url_ext, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

  var blob = response.getBlob().setName(sheet.getName() + '.pdf');

  //from here you should be able to use and manipulate the blob to send and email or create a file per usual.
  //In this example, I save the pdf to drive

  DocsList.createFile(blob);
  //OR DriveApp.createFile(blob);
}

*************************************************

请注意此位:'&portrait=false' + //orientation, false for landscape, true for portrait

这篇关于导出为PDF时更改文档方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆