Amanda T
Amanda T

Reputation: 23

Inserting group of paragraphs into Google Doc at cursor location

I am a newbie to coding and GAS. I have been working on a script for a few days now, after searching countless samples and guides and am stuck!

I am working on creating a rolling or running agenda for my staff meetings. At the start of the doc would be a table of contents based on headers used in the document. Then each week, that week's "entry" would be added. What I want to do is be able to run the script to add the basic entry information to then add to during the meeting so the text doesn't have to be rewritten each time.

What I want to do:

Table of Contents
Entry 1
Entry 2
Entry 3

I want to be able to run the script so Entry 4 would go here

Entry 3
dfafasdfasdfas
afadafadfafd

Entry 2
dfafasdfasdfas
afadafadfafd

Entry 1
dfafasdfasdfas
afadafadfafd

I wrote a script using insertParagraph. I am aware that there is also "appendParagraph", but that always puts the text at the bottom of the document. Problem is, insertParagraph puts the new entry at the very top of the document - above the table of contents. I am looking for a way to insert this text "block" directly after the table of contents.

I have tried changing things so it is a lower class than Paragraph (ie Text using getCursor), but then I can't format using the headings.

Note: I am not trying to insert the Table of Contents with the script - I can do that in the Google Doc and update that there based on the headings, assuming I can keep the headings!

Thank you!!

function AddText() {

var body = DocumentApp.getActiveDocument().getBody();


var date = Utilities.formatDate(new Date(), "EST", "MM-dd-yyyy"); 
var header = body.insertParagraph(0,date);
header.setHeading(DocumentApp.ParagraphHeading.HEADING3);

var section = body.insertParagraph(1,"Members Present");
section.setHeading(DocumentApp.ParagraphHeading.HEADING4);

body.insertParagraph(2,"Name 1, Name 2");

var section = body.insertParagraph(3,"Agenda");
section.setHeading(DocumentApp.ParagraphHeading.HEADING4);

var item1 = body.insertListItem(4,'Discuss Topic (See Program: ProgLink)');

 var url = 'https://www.google.com';

var doc = DocumentApp.getActiveDocument();
var element = doc.getBody().findText("ProgLink");
 if(element){ // if found a match
var start = element.getStartOffset();
var text = element.getElement().asText();
text.replaceText("ProgLink",url);
text.setLinkUrl(start, start+url.length, url);

Logger.log(item1.getListId());

var item2 = body.insertListItem(5,'Trends to Discuss');
item2.setListId(item1);

var item3 = body.insertListItem(6,'Training');
item3.setListId(item1);

var item4 = body.insertListItem(7,'Other Items');
item4.setListId(item1);

}
}

Upvotes: 2

Views: 482

Answers (1)

Cooper
Cooper

Reputation: 64062

Lookin Inside of Google Docs

It installs itself as a sidebar. Hopefully it will work for you. Most of the work I've done with it is with the body. I haven't messed with headers or footers. I don't think you can change the headers for each page but I could be wrong. I had some difficulties getting all of the necessary file together for this tool so now I've put them altogether here.

Upvotes: 1

Related Questions