3

I'm trying to create a simple script that prefills a template into a Google doc at the cursor point. I'm a bit stuck on how to add text and list items at the same time. Here is what I have:

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.');
  }
}

It correctly outputs something like:

Feb 12, 2019

Some Header

 * blue
 * red
 * yellow

Now I want to append yet another simple line of text after the list but if I do it with cursor, it appends it before the date. How can I find the last position of the last item and insert text after it?

1 Answer 1

2
  • You want to put texts and a list to the cursor position.
  • You want to put a text to the last line as follows.
Result you want
Feb 12, 2019

Some Header

 * blue
 * red
 * yellow

sample text <--- here

If my understanding is correct, how about this modification? In this modification, I used the following flow.

  1. Retrieve the child index at the cursor.
    • In this modification, this child index is used as the offset of the body.
  2. Put dateStr, "Some Header\n" and a list to the next child index.
  3. Put a text to the last line.

I think that there are several solutions for your situation. So please think of this as just one of them.

Modified script:

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.');
  }
}

Note:

  • About dateStr, this was used from your script.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, I came to a similar result once I finally figured it out. Thank you!
@mart1n I'm glad your issue was resolved. Thank you, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.