【问题标题】:Inserting template text in a Google doc in Google Apps Script在 Google Apps 脚本中的 Google 文档中插入模板文本
【发布时间】:2019-02-27 10:41:15
【问题描述】:

我正在尝试创建一个简单的脚本,将模板预填充到光标处的 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

现在我想在列表之后添加另一行简单的文本,但如果我使用光标执行此操作,它会在日期之前添加它。如何找到最后一项的最后位置并在其后插入文本?

【问题讨论】:

    标签: javascript google-apps-script google-docs


    【解决方案1】:
    • 您想将文本和列表放在光标位置。
    • 您想将文本放在最后一行,如下所示。
    你想要的结果
    Feb 12, 2019
    
    Some Header
    
     * blue
     * red
     * yellow
    
    sample text <--- here
    

    如果我的理解是正确的,那么这个修改呢?在此修改中,我使用了以下流程。

    1. 检索光标处的子索引。
      • 在这个修改中,这个子索引被用作body的偏移量。
    2. dateStr"Some Header\n" 和一个列表放入下一个子索引。
    3. 将文本放在最后一行。

    我认为针对您的情况有几种解决方案。所以请认为这只是其中之一。

    修改脚本:

    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,这是从您的脚本中使用的。

    参考资料:

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

    • 完美,一旦我终于弄清楚了,我得出了类似的结果。谢谢!
    • @mart1n 很高兴您的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多