[英]Why can't I set custom line spacing to less than zero on a paragraph in google docs using app scripts?
I've been trying to write a script that will output two lines to a google doc.我一直在尝试编写一个脚本,将 output 两行添加到 google 文档。 The lines need to spaced closer together than the default.线条需要比默认间距更近。 In the editor I can click the line spacing icon and select "Custom Spacing" then set it to a value like 0.5 to make the lines closer together.在编辑器中,我可以单击行间距图标和 select“自定义间距”,然后将其设置为 0.5 之类的值,以使线条靠得更近。 I've been trying to achieve the same thing in app scripts, but every time the line spacing value will default to 1. It's almost like my value is being rounded up at some point.我一直在尝试在应用程序脚本中实现相同的目的,但每次行间距值都会默认为 1。这几乎就像我的值在某个时候被四舍五入一样。
This code reproduces my issue:此代码重现了我的问题:
let docBody = DocumentApp.openByUrl("URL OF BLANK GOOGLE DOC").getBody()
let p1 = docBody.appendParagraph("")
p1.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
p1.setLineSpacing(0.5)
let line1 = p1.appendText(`Line 1\r`)
line1.setFontSize(30)
let line2 = p1.appendText("line 2")
line2.setFontFamily("Roboto Mono")
line2.setFontSize(10)
console.log(p1.getLineSpacing())
p1.getLineSpacing returns 0.5 and there are no errors, but in the document the line spacing is always set to 1. If I change 0.5 to something above 1 (like 1.43), it will set the property like it's supposed to. p1.getLineSpacing 返回 0.5 并且没有错误,但在文档中行间距始终设置为 1。如果我将 0.5 更改为大于 1 的值(如 1.43),它将按预期设置属性。 I can also go into the document and change the line spacing normally, but I can't seem to set it from the script.我也可以 go 进入文档并正常更改行间距,但我似乎无法从脚本中设置它。
Is there a reason it can't be set this way?有没有理由不能这样设置?
It seems that in the current stage, the method of setLineSpacing
of Google Document service (DocumentApp) cannot use the custom line space like 0.5
.目前看来,谷歌文档服务(DocumentApp)的setLineSpacing
方法不能使用0.5
这样的自定义行距。 I'm not sure whether this is the current specification or a bug.我不确定这是当前规范还是错误。
But, fortunately, when Google Docs API is used, this custom line space can be used.但是,幸运的是,当使用 Google Docs API 时,可以使用这个自定义行空间。 In this answer, I would like to propose using Google Docs API for setting the custom line space.在这个答案中,我想建议使用 Google Docs API 来设置自定义行间距。
When Docs API is used, your script can be modified as follows.当使用Docs API时,你的脚本可以修改如下。
This script uses Docs API. So, please enable Docs API at Advanced Google services .此脚本使用 Docs API。因此, 请在 Advanced Google services 启用 Docs API 。
function sample() {
const doc = DocumentApp.openByUrl("URL OF BLANK GOOGLE DOC"); // Added
let docBody = doc.getBody(); // Modified
let p1 = docBody.appendParagraph("");
p1.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
// p1.setLineSpacing(0.5); // Removed
let line1 = p1.appendText(`Line 1\r`);
line1.setFontSize(30);
let line2 = p1.appendText("line 2");
line2.setFontFamily("Roboto Mono");
line2.setFontSize(10);
console.log(p1.getLineSpacing());
// I added the below script.
doc.saveAndClose();
const id = doc.getId();
const obj = Docs.Documents.get(id).body.content.pop();
const requests = [{ updateParagraphStyle: { paragraphStyle: { lineSpacing: 50 }, range: { startIndex: obj.startIndex, endIndex: obj.endIndex }, fields: "lineSpacing" } }];
Docs.Documents.batchUpdate({ requests }, id);
}
0.5
on Google Document.运行此脚本时,您可以看到 Google 文档上的行间距设置为0.5
。From this official document, it says as follows.从这个官方文件来看,它是这样说的。
lineSpacing: The amount of space between lines, as a percentage of normal, where normal is represented as 100.0. lineSpacing:线条之间的空间量,以法线的百分比表示,其中法线表示为 100.0。 If unset, the value is inherited from the parent.如果未设置,则该值继承自父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.