Is there a way to eanble Sheet API via Apps Script? - google-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

Related

Auto-generation of a Google doc based on google sheets selection + additional data

Requirement: Auto-generation and completion of a Google doc based on selections made by filtering options in Google sheets
Step-by-step:
There are x options listed in a spreadsheet.
I reduce them to the number I need via filtering.
I then have these options + the expanded details automatically added to a google doc template I've designed.
As an unexperienced programmer, it's very likely that the most convenient way to do something like this is by using Google Apps Script as it has services that help to extend Google Sheets and Google Docs among other Google apps. This service use JavaScript as programming language and it's possible to create and manage the script by using a web browser.
Experienced programmers that don't want to invest too much time on doing this might also find convenient to use Google Apps Script, but they might prefer to use the tools used for other projects might use CLASP or the Google Apps Script API.
Besides using Google Apps Script this could be done by using Google Docs API, Google Sheets API, Google Drive API and Google Cloud.

How do you access the Google Sheets API via a Google Apps Script?

I am trying to call the Google Sheets API via a Google Apps Script. As an example, I have followed the instructions provided here:
https://developers.google.com/sheets/api/quickstart/js
I have generated the client ID and API Key via my Google Cloud Platform project as suggested elsewhere.
Now, I have created a google apps script with two files, the Code.gs file and a file called index.html containing the code copied and pasted from the Google Sheets API Quickstart above (with my client ID and API inserted).
When testing deployment, I get the following error message:
{
"error": "idpiframe_initialization_failed",
"details": "Not a valid origin for the client: https://<some numbers>-script.googleusercontent.com has not been registered for client ID <MY_CLIENT_ID>. Please go to https://console.developers.google.com/ and register this origin for your project's client ID."
}
I have registered script.google.com as an origin for my project.
Am I doing something obviously wrong? How would I proceed from here to get past this error?
If you want to make use of the Sheets API in Apps Script you will have to make use of the Sheets Advanced Service.
For this you will have to go to Services + and then add the Google Sheets API:
Since this is an advanced service, the authorization flow will be handled by Google so you won't have to provide the clientId and the API key.
Reference
Apps Script Advanced Google Services.

Can anything done with google apps scripts be done with the google API?

I noticed that the Google Apps Script editor uses a Bearer token to make requests for a lot of things, that got me thinking: Is it possible to do all functions that are done with the google apps script with the google API on your own server? What is the essential difference? Just the fact that google apps script runs on google servers, while with the google API you would need to create your own servers?
For example many of the document functions, which I thought could only be done with the API, turns out can be found here https://developers.google.com/docs/api/reference/rest/v1/documents/request#InsertInlineImageRequest
so whats the essential difference?
Just the fact that google apps script runs on google servers, while with the google API you would need to create your own servers?
Yes. There are advantages to both methods:
API:
It's a rest API. You can use your own language(eg: Python) in your servers.
The api is more inclusive. There are stuff which can be done with the api, which cannot be accomplished with apps script.
User can set limited scopes for using API.
For example, at Drive API, https://www.googleapis.com/auth/drive.file can be used for Drive API. The same cannot be done with DriveApp of apps script.
Process cost of API is lower than that of the built-in methods for Google Apps Script1 2
Apps script:
No server on your side
Triggers. You can set up functions to run onEdit,onOpen or at a specific time.
Deep integration with Google apps: Sidebars/modal dialogs can only be done with apps script.
All methods of the api can be accessed indirectly through the bearer token.
Authorization/authentication is taken care of by apps script. You don't need to set up oauth.

Standalone Google App Script Invocation from Google Sheets

I have refactored my bound GAS script into a standalone script (for re-use) and decoupled the google sheets specific calls. I would like to be able to invoke the standalone google app script from within a bound Google Sheets script, is there a way to do this?

Using oAuth2 to send requests to Google Apps Script (i.e. GAS as google API)

I have been looking for a way to interact with my google sheets (and other services) from Excel (and other desktop applications).
For example:
I get a report from a work system and my boss asks me to update the information in the google sheets so that our site is up to date with the latest information.
What would be great (and easy for me) would be to use a VBA script in my spreadsheet to turn all my data in http requests and then do the heavy lifting with Google Apps Script in the cloud and finally updating the Google Sheets with the associated API.
When I look at the oAuth2 playground I can't find a way to enable Google Apps script, as I have successfully done so with other APIs.
(to be clear, I don't want to do oAuth2 with in GAS - which is a lot of the other questions that get raised)
Is this a hard problem or is there a solution?
There is currently no way of calling a Google Apps Script from an external program. You do have a few other options:
Import the relevant data into Google Sheets manually and use Google Apps Script to process it as needed, possibly making use of time-based triggers to handle the processing automatically.
Use the Google Spreadsheets API. Unlike Apps Script, this API is built to be called from other programs and can manipulate Google Sheets. However, it is a bit more difficult to use than Apps Script. Once data has been moved into a Google Sheet, a separate Apps Script can be used to manipulate it.