Question: Here is my goal: in a Google Document, I want to be able to run a Google Script to populate a section of the Document with the upcoming events from a couple of specific named Google Calendars.

Is this possible?

1. Can I run a script inside a Document? From a button or menu item? I haven't found documentation about how to do this.
2. In the script, can I target a specific section of the document to be replaced with the new content? Is there an ID on every Element, or could I add a custom attribute to the target Element?

Thanks.

**Edit:**

I've solved #2 and have everything running as I want it to. From the Google Script editor, I can target a specific Document and push the desired content to the desired location.

All that is left is to hook it into any document, so that I can run the script from the document.

**Edit:**

For #1, it looks like the answer is that Google Documents cannot host scripts. But I can make a general Script, and from it I can open and manipulate any of my Documents.

Answer: 
Do you need it to run from the Document? You're looking for a list of coming events? Why not do it as a web app? You could pair it up with an HTML template file, and use the [HTMLService](https://developers.google.com/apps-script/html_service) to generate the end result, after building a string with the events.
For #2, I search the tables in the document, looking for specific text in the first cell of the table. So this routine gets me the table I need to update:

```js
var targetFirstCellText = 'Date';

function findTableInDoc(docId){
  var doc = DocumentApp.openById(docId);
  var tables = doc.getTables();
  var table = null;
  var found = false;
  for(var t=0;!found && t<tables.length;t++){
    table = tables[t];
    var firstCellText = table.getCell(0,0).getText();
    if(firstCellText==targetFirstCellText){
      found = true;
    }
  }
  return table;
}

```

Then, before I start adding new rows, I run this. (Could be done in other ways... this was quick and easy.)

```
while(table.getNumRows()>1){
  table.getRow(1).removeFromParent();
}

```