在 Google Apps 脚本中的 Google 文档中插入模板文本
Inserting template text in a Google doc in Google Apps Script
我正在尝试创建一个简单的脚本,将模板预填充到 Google 文档中的光标位置。我对如何同时添加文本和列表项有些困惑。这是我拥有的:
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Templates')
.addItem('Insert Template', 'insertTemplate')
.addToUi();
}
function insertTemplate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
var element = cursor.insertText("Some Header\n")
element.setBold(true);
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
var today = new Date();
dateStr = today.toLocaleDateString("en-US", options) + '\n\n';
var element = cursor.insertText(dateStr);
element.setBold(true);
var body = DocumentApp.getActiveDocument().getBody();
colors = ["blue", "red", "yellow"]
colors.forEach( function(color) {
body.appendListItem(color + ": ").setGlyphType(DocumentApp.GlyphType.BULLET);
});
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
它正确输出如下内容:
Feb 12, 2019
Some Header
* blue
* red
* yellow
现在我想在列表之后再追加一行简单的文本,但是如果我用光标来做,它会追加到日期之前。如何找到最后一项的最后位置并在其后插入文本?
- 您想将文本和列表放在光标位置。
- 您想在最后一行添加一段文字,如下所示。
你要的结果
Feb 12, 2019
Some Header
* blue
* red
* yellow
sample text <--- here
如果我的理解是正确的,这个修改怎么样?在这次修改中,我使用了以下流程。
- 检索光标处的子索引。
- 在本次修改中,此子索引用作正文的偏移量。
- 将
dateStr
、"Some Header\n"
和列表放入下一个子索引。
- 在最后一行添加文字。
我认为针对您的情况有多种解决方案。所以请把这当作其中之一。
修改后的脚本:
function insertTemplate() {
var doc = DocumentApp.getActiveDocument(); // Added
var cursor = doc.getCursor(); // Modified
if (cursor) {
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
var today = new Date();
dateStr = today.toLocaleDateString("en-US", options) + '\n\n';
var body = doc.getBody();
// Below script was modified.
var offset = body.getChildIndex(cursor.getElement());
body.insertParagraph(offset, dateStr);
body.insertParagraph(++offset, "Some Header\n");
colors = ["blue", "red", "yellow"]
colors.forEach(function(color, i) {
body.insertListItem(++offset, color + ": ").setGlyphType(DocumentApp.GlyphType.BULLET);
});
body.insertParagraph(--offset + colors.length, "sample text"); // Please set the text here.
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
注:
- 关于
dateStr
,这是从您的脚本中使用的。
参考文献:
如果我误解了您的问题而这不是您想要的结果,我深表歉意。
我正在尝试创建一个简单的脚本,将模板预填充到 Google 文档中的光标位置。我对如何同时添加文本和列表项有些困惑。这是我拥有的:
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Templates')
.addItem('Insert Template', 'insertTemplate')
.addToUi();
}
function insertTemplate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
var element = cursor.insertText("Some Header\n")
element.setBold(true);
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
var today = new Date();
dateStr = today.toLocaleDateString("en-US", options) + '\n\n';
var element = cursor.insertText(dateStr);
element.setBold(true);
var body = DocumentApp.getActiveDocument().getBody();
colors = ["blue", "red", "yellow"]
colors.forEach( function(color) {
body.appendListItem(color + ": ").setGlyphType(DocumentApp.GlyphType.BULLET);
});
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
它正确输出如下内容:
Feb 12, 2019
Some Header
* blue
* red
* yellow
现在我想在列表之后再追加一行简单的文本,但是如果我用光标来做,它会追加到日期之前。如何找到最后一项的最后位置并在其后插入文本?
- 您想将文本和列表放在光标位置。
- 您想在最后一行添加一段文字,如下所示。
Feb 12, 2019
Some Header
* blue
* red
* yellow
sample text <--- here
如果我的理解是正确的,这个修改怎么样?在这次修改中,我使用了以下流程。
- 检索光标处的子索引。
- 在本次修改中,此子索引用作正文的偏移量。
- 将
dateStr
、"Some Header\n"
和列表放入下一个子索引。 - 在最后一行添加文字。
我认为针对您的情况有多种解决方案。所以请把这当作其中之一。
修改后的脚本:
function insertTemplate() {
var doc = DocumentApp.getActiveDocument(); // Added
var cursor = doc.getCursor(); // Modified
if (cursor) {
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
var today = new Date();
dateStr = today.toLocaleDateString("en-US", options) + '\n\n';
var body = doc.getBody();
// Below script was modified.
var offset = body.getChildIndex(cursor.getElement());
body.insertParagraph(offset, dateStr);
body.insertParagraph(++offset, "Some Header\n");
colors = ["blue", "red", "yellow"]
colors.forEach(function(color, i) {
body.insertListItem(++offset, color + ": ").setGlyphType(DocumentApp.GlyphType.BULLET);
});
body.insertParagraph(--offset + colors.length, "sample text"); // Please set the text here.
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
注:
- 关于
dateStr
,这是从您的脚本中使用的。
参考文献:
如果我误解了您的问题而这不是您想要的结果,我深表歉意。