javascriptgoogle-docschecklistbox

Place Header with bulleted List After Specific Text in Google Doc Using Script


I'm attempting to have a button (linked image or text) users can use to insert a templated paragraph of text into a specific section of a google doc. The templated text is a Header which has a checklist checkbox (when used, strikes out line), and includes an expandable bulleted list. Example of the text script should input

I've mostly been able to build out how I want the template text to look, but haven't found a solution online for placing it after specific text while remaining formatted, and also making the header a checklist. The text it should appear under is "New Items:".

I've found ways to place unformatted text in specific areas, and ways to create lists that are all checkboxes, but nothing that includes the combinations I'm looking for.

I've found ways to place the cursor in a specific location and ways to "findtext", but none that I was able to make work along with the specific formatted text I'm using.

Here's an example google doc that includes the script I have so far.

Here's the script so far:

function insertText() {
  var headerFormatOn = {"BOLD": true, "BACKGROUND_COLOR": "#000000", "FOREGROUND_COLOR": "#FFFFFF"};
  ///var formatOn = {"BOLD": true, "BACKGROUND_COLOR": "#000000", "FOREGROUND_COLOR": "#FFFFFF"};
  ///var formatOff = {"BOLD": false, "BACKGROUND_COLOR": "#000000", "FOREGROUND_COLOR": "#FFFFFF"};
  var header = "Enter_Item_Title_Here"
  var bulletBody = ": ___________"
  var bulletOneHeader = "Agenda Item Owner(s): ___________"
  var bulletTwoHeader = "Discussant: ___________"
  var bulletThreeHeader = "Discussion Date: ___________"
  var bulletFourHeader = "External follow up: ___________"
  var bulletFiveHeader = "Notes: ___________"
  var bulletSixHeader = "Action Items: ___________"

  var attributes1 = Object.entries(headerFormatOn).reduce((o, [k, v]) => Object.assign(o, {[k]: v}), {});
  ///var attributes2 = Object.entries(formatOn).reduce((o, [k, v]) => Object.assign(o, {[k]: v}), {});
  ///var attributes3 = Object.entries(formatOff).reduce((o, [k, v]) => Object.assign(o, {[k]: v}), {});

  var cursor = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();
  var text1 = cursor.appendParagraph(header);
  text1.setHeading(DocumentApp.ParagraphHeading.HEADING3);
  ///var text2 = cursor.insertText(bulletBody);
  ///text2.setAttributes(attributes3);
  var text3 = cursor.appendListItem(bulletOneHeader);
    //text3.setAttributes(attributes2);
    text3.setGlyphType(DocumentApp.GlyphType.BULLET).setGlyphType(DocumentApp.GlyphType.BULLET);

  var text4 = cursor.appendListItem(bulletTwoHeader);
    //text4.setAttributes(attributes2);
    text4.setGlyphType(DocumentApp.GlyphType.BULLET);
  var text5 = cursor.appendListItem(bulletThreeHeader);
  text5.setGlyphType(DocumentApp.GlyphType.BULLET);
  var text6 = cursor.appendListItem(bulletFourHeader);
  text6.setGlyphType(DocumentApp.GlyphType.BULLET);
  var text7 = cursor.appendListItem(bulletFiveHeader);
  text7.setGlyphType(DocumentApp.GlyphType.BULLET);
  var text8 = cursor.appendListItem(bulletSixHeader);
  text8.setGlyphType(DocumentApp.GlyphType.BULLET);
  var text9 = cursor.appendListItem(bulletSixHeader);
  text9.setGlyphType(DocumentApp.GlyphType.BULLET);
}

I have a feeling some of it might have to do with "append" vs "insert", or some of the variations I've read about in my searching, but I haven't been able to figure out which pieces need to change in order for it to work.


Solution

  • You are correct with the statement I have a feeling some of it might have to do with "append" vs "insert". From what I know, appendParagraph(paragraph) and appendListItem(listItem) are meant to add on something at the end of the document, while insertParagraph(childIndex, paragraph) and insertListItem(childIndex, listItem) are to add something wherever you'd like, in this case after the "New Items" text.

    Here's a modified version of your script that should achieve what you'd like:

    function insertText() {
      var header = "Enter_Item_Title_Here"
      var bulletOneHeader = "Agenda Item Owner(s): ___________"
      var bulletTwoHeader = "Discussant: ___________"
      var bulletThreeHeader = "Discussion Date: ___________"
      var bulletFourHeader = "External follow up: ___________"
      var bulletFiveHeader = "Notes: ___________"
      var bulletSixHeader = "Action Items: ___________"
      var cursor = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();
      var pr = cursor.findText("New Items:").getElement().getParent();
      var i = cursor.getChildIndex(pr) + 1;
      cursor.insertParagraph(i, header).setHeading(DocumentApp.ParagraphHeading.HEADING3);
      cursor.insertListItem(i + 1, bulletOneHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
      cursor.insertListItem(i + 2, bulletTwoHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
      cursor.insertListItem(i + 3, bulletThreeHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
      cursor.insertListItem(i + 4, bulletFourHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
      cursor.insertListItem(i + 5, bulletFiveHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
      cursor.insertListItem(i + 6, bulletSixHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
    }
    

    Note: I removed the unused lines from the code, and the output should be similar to the formatting that you want.