Get Bounded Script ID from another project - google-apps-script

Is there a way to get the id of a script embedded in a spreadsheet (not the id of the spreadsheet) from another script (in other file)?
I have a main standalone script that copies a spreadsheet that contains a script (web app code). I need to get the id of this script and use it to create a deployment with the apps script API REST.
Is there any other way to programmatically deploy a bounded web app script in a spreadsheet?
Apologies for my bad English, I´m using the translator

In order to achieve Is there any other way to programmatically deploy a bounded web app script in a spreadsheet?, at first, it is required to retrieve the script ID of the container-bound script.
But, unfortunately, in the current stage, the script ID of the container-bound script cannot still be retrieved from outside of the project using only Google Apps Script. And also, the the script ID of the container-bound script cannot be retrieved from the file ID of the parent Google Docs file. This has already been reported to the Google issue tracker as the Feature Request. Ref
When the script ID of the container-bound script can be retrieved, you can use "REST Resource: projects.deployments" in Apps Script API.
Note:
If the script for retrieving the script ID is in the same project, the script ID can be retrieved using ScriptApp.getScriptId().
The file ID of Google Docs can be retrieved from the script ID of the container-bound script using the method of "Method: projects.get" in Apps Script API. But in the current stage, the contrary cannot be done.
References:
getScriptId()
Method: projects.get
REST Resource: projects.deployments

Related

Is there a way to eanble Sheet API via Apps Script?

I've exploring possibilities here to dimish the workload for users when first running the sets of script I'd have built into the spreadsheet.
One of the functions requires Sheets API and having the user manually enable it would be a killer. I've read through it and couldn't find a clear answer as to the possibility to add this advanced service together via script when the user is first authorizing the script to run...
This is what the user would have to do instead:
Thanks for your input!
Although, unfortunately, from your question, I cannot know your actual situation, in order to achieve your goal, I thought of the following 3 patterns.
Pattern 1:
In this pattern, Sheets API is directly used.
If the Google Apps Script project (GAS project) is the container-bound script, it adds Sheets API to appsscript.json of the GAS project using Google Apps Script API.
In this case, it is required to link Google Cloud Platform Project to Google Apps Script Project. Ref
If the Google Apps Script project (GAS project) is the standalone script, it adds Sheets API to appsscript.json of the GAS project using Drive API.
By this, Sheets API can be directly used with the GAS project.
Pattern 2:
In this pattern, Sheets API is directly used. The flow of this pattern is as follows.
Prepare a Google Spreadsheet including the container-bound script.
The container-bound script has the script you want to use and Sheets API has already been enabled.
When you want to make users use this script, by copying this Spreadsheet, the user can use the script that Sheets API has already been enabled.
I thought that this pattern might be simpler.
Pattern 3:
In this pattern, Sheets API is indirectly used. The flow of this pattern is as follows.
Create a new Google Apps Script project and enable Sheets API at Advanced Google services.
Deploy the GAS project as the Web Apps.
When you use Sheets API at your GAS project, you can indirectly use Sheets API by accessing Web Apps using the script.
In this case, Sheets API has already been enabled at the Web Apps side. So, your GAS project of the client side is not required to enable Sheets API.
In this case, also, I think that it is required to check the number of concurrent accesses. Ref
References:
REST Resource: projects of Google Apps Script API
Files: update of Drive API
Web Apps

How can I set an app script on a Google Sheet via the API?

Is there a way to set an App Script on a new Google Sheet via the Google Sheets API? We use pygsheets as our Python API Client for Google Sheets. Since we are using the onSelectionChange trigger to populate certain cells with a date, we would like to setup the app script after we have populated rows in the spreadsheet.
The Google Sheets API doesn't have a method to do this. You can use the Apps Script API instead, but there are a few caveats which may or may not work with your use case:
You can use projects.create to create a new Apps Script project and attach it to a Sheets file by specifying the parentId field, which is the Sheet's ID. However, there are no methods to attach a preexisting script to a Sheet. You can still use projects.updateContent to add the required script files, though.
You cannot unbind the script once it has been attached.
This API does not work with service accounts, and the user running it needs to explicitly allow it in their settings page. This is for security reasons, since allowing a malicious app to freely write scripts on your account could be catastrophic, but it adds more steps that your users would need to take in order to run your app.

Multiple files in Google App Script My Project

I'm new to this google script thing so I'm not sure what should I do with this. So, I'm working with 8 folders with files with the same structure and scripts. So, when I opened my projects on the Google App Script. I currently have 178 projects. I just want to ask if the picture below is a normal thing or is there a way for me to minimize it since some of it has the same scripts? Can I use one project in multiple sheets?
As you can see in the picture below, I do have 8 ARCS_AP_ROWS since I duplicate the spreadsheet 8 times. The scripts in there were all the same.
The reason you are having a separate script for each spreadsheet is because you created container-bound scripts.
What are container-bound scripts?
According to the Apps Script documentation:
A script is bound to a Google Sheets, Docs, Slides, or Forms file if it was created from that document rather than as a standalone script. The file a bound script is attached to is referred to as a "container". Bound scripts generally behave like standalone scripts except that they do not appear in Google Drive, they cannot be detached from the file they are bound to, and they gain a few special privileges over the parent file.
TL;DR - they're scripts created for one particular document.
So even though you essentially have the same script, the document on which the script acts is different.
What you can do
Create a standalone script and use SpreadsheetApp.openById("SPREADSHEET_ID") and pass the SPREADSHEET_ID parameter in order to open a specific spreadsheet;
Create an add-on.
Reference
Container-bound Scripts;
Standalone Scripts;
Apps Script SpreadsheetApp Class - openById(id);
Google Workspace Add-ons.

How to get Google Apps Script project ID linked/bound to a Google Spreadsheet

I have a task to update Apps Scripts linked to some Spreadsheets using Google REST APIs. I know how to update script if I have script Id, but the problem is that I have only spreadsheet Id, and I need to updated linked/bound script.
I didn't find any method in Apps Script REST API that would allow listing scripts linked to a spreadsheet https://developers.google.com/apps-script/api/reference/rest/
I also tried Google Drive REST APIs https://developers.google.com/drive/api/v2/reference/children/list and https://developers.google.com/drive/api/v2/reference/files/list with no luck.
Currently, it is not possible to retrieve the bound script id from the parent spreadsheet id. You can star(star on top left) these issues to prioritize these issues:
https://issuetracker.google.com/issues/111149037
https://issuetracker.google.com/issues/117105106
Depending on your use case, There's a workaround mentioned by Tanaike here

How to import source files into google apps script project embedded in a google spreadsheet

I'm able to import code into a standalone google apps script by following the instruction here, but I got a problem when tried to import code into a script embedded in a spreadsheet for the script didn't have a file id but only a project id.
So how can we know the file id of a script that embedded in a google apps script, so that we can import code into it with its file id?
Reading through the same link you provided, it appears that Container-bound scripts (script embedded in a spreadsheet) cannot be accessed through the Google Drive REST API. It's stated in Features and Limitations, number 6.
An alternative to this would be to use Appscript Add-ons.