Access to the 'imported' document appears to be determined by the sharing settings on the imported document at the time the 'host' document (not the template) is created.
For example:
- Template A uses importrange() to include data from document B
- Sharing on document B is set to anything other than "Anyone on the Internet with this link can view"
- Template A is used to create document C
Document C will not be able to import the content from document B. You will have to manually allow access.
If, before you manually allow access, you close document C, change the sharing on B to 'Anyone on the Internet with this link can view' and then reopen document C it will still fail to import the data from B. When document C was created it did not have access to document B for import, therefore it will never have access unless you manually grant access.
On the other hand:
- Template A uses importrange() to include data from document B
- Sharing on document B is set to "Anyone on the Internet with this link can view"
- Template A is used to create document C
Now document C can import the data from document B with no problem.
If you now change the sharing on document B to 'Restricted' document C will continue to import data. Similar to the first case, when document C was created it had access to document B, therefore it will always have access.
The importrange() uses the sharing as it is for the imported document at the time the host document is created.
Based on this and the code provided by Adrian Mole I set up a workflow for creating my documents from templates as follows:
In a Google Apps script run through list of included document ids and set sharing to public:
var idList = { "doc-id-1", "doc-id-2", "doc-id-3", [...] };
for( id=0; id < idList.length; id++ ) {
DriveApp.getFileById(id).setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
}
Create multiple documents from the template
Back in Google Apps script use similar code to close access to all the imported documents:
var idList = { "doc-id-1", "doc-id-2", "doc-id-3", [...] };
for( id=0; id < idList.length; id++ ) {
DriveApp.getFileById(id).setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT);
}
The documents I create can all import data correctly (each contains multiple importrange() functions) but when I am done all the imported documents are fully secured again.
Note that between steps 1 and 3 while the documents are being created from templates the imported documents are all publicly searchable and available on the Internet. It is theoretically possible for a malicious person to create an importrange() in a spreadsheet of their own to import your documents and they would continue to have that access after performed step 3 and 'locked' your documents again.