简体   繁体   English

为什么我不能使用应用程序脚本在 google 文档中的段落中将自定义行间距设置为小于零?

[英]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?有没有理由不能这样设置?

Issue and workaround:问题和解决方法:

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时,你的脚本可以修改如下。

Modified script:修改脚本:

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);
}
  • When this script is run, you can see that the line space is set as 0.5 on Google Document.运行此脚本时,您可以看到 Google 文档上的行间距设置为0.5

References:参考:

  • UpdateParagraphStyleRequest 更新段落样式请求

  • ParagraphStyle 段落样式

    • 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.

相关问题 使用Google App脚本检索Google文档中使用的HTML代码 - Retrieving HTML code used in google docs using google app scripts 如何使用 App Scripts 从 Google Docs 中删除文本? - How to remove text from Google Docs using App Scripts? 如何使用 Google Apps Script 在 Google Docs 中的段落之前设置一定数量的空格或缩进 - How to set a certain number of spaces or indents before a Paragraph in Google Docs using Google Apps Script 为什么不能在Google脚本编辑器中导入库? - Why can't I import a Library in Google Scripts editor? 如何使用 App Script 在 Google Docs 中选择一个空行? - How do I select an empty line in Google Docs using App Script? 如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色? - How can I set the background color of a row in a table using google apps script in Google Docs? 使用 Google Apps 脚本设置列表行之间的行距 - Set line spacing between lines of a list using Google Apps Script 应用程序脚本 web 如果匿名,应用程序无法访问谷歌文档 - app script web app can't access google docs if anonymous 如何通过App Scripts在Google Docs的文档正文中的特定单词之后设置cursor? - How to set the cursor after a specific word in doc body in Google Docs through App Scripts? 为什么谷歌电子表格脚本无法排序? - Why google spreadsheets scripts can't sort?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM