Insert an AppScript function into newly automatically created spreadsheets

628 views
Skip to first unread message

Parham Pajouhi

unread,
Mar 31, 2022, 8:03:31 AM3/31/22
to Google Apps Script Community
Hello everyone!
Do you know a way to insert an Apps Script function into newly built spreadsheets that is created via App Script?

My code makes multiple SpreadSheets and I want to collect data from them. I need to insert my block of code into those separate spreadsheets in order the collect the data when the recipient is finished and clicked on the UI button.

Than you so much in advanced!
Keep coding!
Parham

Sandi Berg

unread,
Mar 31, 2022, 8:21:30 AM3/31/22
to google-apps-sc...@googlegroups.com
When I do this, I have a template spreadsheet with the code in it and make a copy of the template. The code is copied with it.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6a51d4a2-9fac-4e50-a864-55c2b63f0ddan%40googlegroups.com.

Parham Pajouhi

unread,
Mar 31, 2022, 8:26:32 AM3/31/22
to Google Apps Script Community
Hey Sandi,
Thank you for your reply. That is the only solution I came up with as well. I thought that it would be convenient to add the code to the spreadsheet somehow! But it looks like that we don't have such a functionality and the only way is that I change the structure and approach that I have now and go with Template SpreadSheet solution.
Thanks for sharing.

Sandi Berg

unread,
Mar 31, 2022, 9:58:50 AM3/31/22
to google-apps-sc...@googlegroups.com
You can push the code manually to each document if you know the app script id code.

Parham Pajouhi

unread,
Mar 31, 2022, 10:32:11 AM3/31/22
to google-apps-sc...@googlegroups.com
Yeah, but doing things manually is not an option! The code will generate +200 spreadsheets every month with a lot of data in it. I came up with a better solution to capture data from the individual spreadsheets and it's the IMPORTRANGE function! So brilliant! I have all the data in real time. The only downside of it was to do the "Allow access" for every spreadsheet to settle the link between the two. I solved that problem with the magic of code and things are happening in real time! 

Thank you for your replies Sandi, you're awesome!

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/hIlUZuLYGmg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAB807N0QaTmZ6VzLi3w8LvRZikC%3DobMzny%3DdyyF2%2BDDJwwkCjw%40mail.gmail.com.


--

Parham Pajouhi

Productivity Engineer

Productivity Efficiency

+46 73-054 79 36


Klarna Bank AB (publ)

Sveavägen 46, 111 34 Stockholm

Tel: +46 8 120 120 00

Reg no: 556737-0431

klarna.com



Bruce Mcpherson

unread,
Mar 31, 2022, 10:33:11 AM3/31/22
to google-apps-sc...@googlegroups.com
Hi
This shows how to automate importing a script into a container.

--

Parham Pajouhi

unread,
Mar 31, 2022, 10:36:01 AM3/31/22
to google-apps-sc...@googlegroups.com
Hi Bruce, 
Thanks. I'll have a look at it now! 

Sandi Berg

unread,
Mar 31, 2022, 7:38:05 PM3/31/22
to google-apps-sc...@googlegroups.com
I was using importrange but found it slowed things down because it refreshes so often...plus the whole "must activate"... however, I would love if you shared the code for activation so I know how to do it in the future.

Parham Pajouhi

unread,
Apr 1, 2022, 7:45:07 AM4/1/22
to google-apps-sc...@googlegroups.com
This piece approves all the separated spreadsheets automatically!

//Pre confirm the links between two spreadsheets for the IMPORTRANGE function
            function addImportrangePermission() {
              const donorId = 'The ID of the spreadsheet with data';
              const ssId ='The ID of the spreadsheet that data will be imported to';
              const url = `https://docs.google.com/spreadsheets/d/${ssId}/externaldata/addimportrangepermissions?donorDocId=${donorId}`;
              const token = ScriptApp.getOAuthToken();
              const params = {
                method: 'post',
                headers: {
                  Authorization: 'Bearer ' + token,
                    },
                  muteHttpExceptions: true
                  };
                UrlFetchApp.fetch(url, params);
            }

And that's it! 
By the way, I changed the structure and I'm using the Template Spreadsheet way. 
All the best.
P
Reply all
Reply to author
Forward
0 new messages