Answer:
In order to run a DocumentApp
script on edit of a Spreadsheet, the On edit
installable trigger attached to the Spreadsheet must be used.
More Information:
As per the Simple Triggers documentation, there are some restrictions which need to be taken into account. In particular:
They can modify the file they are bound to, but cannot access other files because that would require authorization.
As a result, the onEdit(e)
trigger function can not be used. There is however an installable trigger which can be created, with settings set up such that it can fire on edit.
Code:
With the following function in the script bound to the Spreadsheet file:
function updateDocument() {
var doc = DocumentApp.openById('DOCUMENT_ID');
// here you can put your code that edits the document in the way you want.
}
You can create an installable trigger which runs on the edit of the Spreadsheet. You will need to run the code at least once before setting up the trigger however - this is because DocumentApp
needs authorisation and you need to grant it!
Setting up an Installable Trigger:
With the code set up, you can create the installable trigger by completing the following steps:
From the Apps Script editor view for the bound Spreadsheet script, follow the path Edit > Current project's triggers
. This will open the triggers for the project in a new tab or window.
In the bottom left, click on the + Add Trigger
button, bringing up the Add Trigger
modal. From here, set up the trigger with the following properties:
- Choose which function to run:
updateDocument
- Choose which deployment should run:
Head
- Select event source:
From spreadsheet
- Select event type:
On edit
And click Save
. This will set up the trigger such that your Document editing function will run each time the Spreadsheet is edited.
I hope this is helpful to you!
References:
I want to trigger that DocumentApp script to run whenever the SpreadsheetApp is edited in any way.
. Can I ask you about it? By the way, I think thatopenById
cannot be used with the simple trigger usingonEdit()
. If you want to useopenById
by the OnEdit event trigger, please use the installable OnEdit event trigger. How about this? - Tanaikefunction updateDocumentOnEditTrigger() { var ss = SpreadsheetApp.openById(SheetID); ScriptApp.newTrigger('UpdateDocument') .forSpreadsheet(ss) .onEdit() .create(); }
I wrapped the other function inside this function and placed it outside and neither seems to update the DocumentApp file. - CatalyxonEdit
trigger on the SpreadsheetApp that will then update the DocumentApp. So that anytime the SpreadsheetApp is edited, code will run that will then edit/update the DocumentApp. Is that right? - IMTheNachoMan