我通过 Google Apps 脚本生成文档。
有没有办法将页面设置...
> 方向
从Portrait
更改为Landscape
?
文档包含的表格只能在横向
模式下显示良好,我希望能够:
- 生成文档
- 将方向从纵向更改为横向
- 将文档转换为 PDF 并发送给用户
转换为 PDF 并发送
// create the document
var doc = DocumentApp.create('Sample Document - Landscape Mode');
var body = doc.getBody();
// add lots of content...
// ensure all content is added to the PDF
// see: https://stackoverflow.com/a/44481513/1063287
doc.saveAndClose();
// create the pdf file
var pdf_file = doc.getAs("application/pdf");
// see: https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
GmailApp.sendEmail('user@test.com', 'Attachment example', 'Please see the attached file.', {
attachments: [pdf_file],
name: 'Test Name'
});
如果没有官方支持,我很想知道是否有任何 hacky 方法可以实现相同的效果(比如:url 参数强制横向模式、pdf 文件的编程旋转、获取 Google Apps 脚本来创建页面默认处于横向模式?)
编辑:
在 Page setup...
> Orientation
中,有一个选项可以Set as default
。
我点击它并运行脚本,它以 Landscape
模式创建文档,然后以 Landscape
模式发送 PDF,这很棒,但它只是意味着每当我想在 Portrait
模式下手动创建文档时,我必须手动将 Orientation
更改为 Portrait
。此外,如果我想要另一个函数以 Portrait
模式创建文档,则此解决方案将不起作用。
所以有一些进步,但还是想知道是否有更全面的解决方案。
编辑 2:
我在浏览器中以 Portrait
查看文档时尝试了这种方法:
https://stackoverflow.com/a/35344694/1063287
即,我替换了:
/edit
在 url 的末尾:
export?exportFormat=pdf&format=pdf&portrait=false
但下载的文件是Portrait
方向。
最佳答案
当我遇到你的问题时,我正在研究完全相同的事情,并最终找到了答案,所以我不妨在这里分享一下。 这个链接很好地解释了一个解决方案: https://productforums.google.com/forum/#!topic/docs/HuwBipQ6cBY
您需要在代码中手动设置纸张的尺寸(以磅为单位)。 上面链接中的代码使用小数形式的尺寸。 不过,我确实在这里使用整数找到了一些推荐的测量值: https://www.prepressure.com/library/paper-size/din-a4
这就是我创建新文档并将其设置为 A4 横屏的方式:
var doc = DocumentApp.create("Document name");
var docBody = doc.getBody();
docBody.setPageHeight(595).setPageWidth(842);
这里引用了第一个链接中使用十进制数字的值。
引用第一个链接:
Rotate to landscape, A4 size
doc.getBody().setPageHeight(595.276).setPageWidth(841.89);
Rotate to portrait, A4 size
doc.getBody().setPageHeight(841.89).setPageWidth(595.276);
希望对您有所帮助。
关于google-apps-script - 如何在 Google Apps 脚本中将页面设置方向从纵向更改为横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480451/