【发布时间】:2019-04-11 21:05:06
【问题描述】:
我有一个 20 行的脚本,我想确保每个段落都缩进一次。
function myFunction() {
/*
This function turns the document's format into standard MLA.
*/
var body = DocumentApp.getActiveDocument().getBody();
body.setFontSize(12); // Set the font size of the contents of the documents to 9
body.setForegroundColor('#000000');
body.setFontFamily("Times New Roman");
// Loops through paragraphs in body and sets each to double spaced
var paragraphs = body.getParagraphs();
for (var i = 3; i < paragraphs.length; i++) { // Starts at 3 to exclude first 4 developer-made paragraphs
var paragraph = paragraphs[i];
paragraph.setLineSpacing(2);
// Left align the first cell.
paragraph.setAlignment(DocumentApp.HorizontalAlignment.LEFT);
// One indent
paragraph.editAsText().insertText(0, "\t"); // Adds one tab every time
}
var bodyText = body.editAsText();
bodyText.insertText(0, 'February 3, 1976\nMrs. Smith\nYour Name Here\nSocial Studies\n');
bodyText.setBold(false);
}
我尝试过的代码不起作用。但我的预期结果是,对于myFunction() 中for 循环中的每一段,每一段的第一个单词前正好有4 个空格。
这是一个示例:https://docs.google.com/document/d/1sMztzhOehzheRdqumC6PLnvk4qJgUCSE0irjTZ0FjTQ/edit?usp=sharing
更新
我调查了Paragraph.setIndentFirstLine() 方法的使用。当我将其设置为 4 时,它会将其设置为 1 个空格。现在我意识到这是因为点和空间不是一回事。我需要乘以多少才能得到四个空格?
【问题讨论】:
-
感谢您的回复。我无法理解
But not just the top paragraph (actual paragraph not Mrs. Smith...) every paragraph, hence the for loop looping through the paragraphs.。我为我糟糕的英语水平道歉。 -
为什么不使用Paragraph类的标准方法getIndentStart()和getIndentFirstLine()?
-
@АлександрЕрмолин 现在我再次对其进行了测试,这非常有效。当我第一次尝试时它不起作用,但由于某种原因现在它起作用了。
-
@АлександрЕрмолин 再三考虑,这并不完美:(
标签: google-apps-script google-docs paragraph