I have developed few scripts in a given project attached to a spreadsheet (ie that I first created my spreadsheet, and then created the project with the menu Tools>Script editor).
Now my question is:
how can I save that project so that it can be used on other spreadsheets?
how do I select in a given spreadsheet which script project I want to use?
From this Google documentation page I can't see any solution.
To have a script that's not bound to any document, look into making a standalone script.
If you want to use a script in multiple documents, you'll need to publish it as a library.
Related
I have scenario where I have more than 60 sheet, and all have appscript project behind them running and managing those sheet.
I want to updat/control those appscript projects all at once, from one different project. So that I don't have to manually go and update them.
I know this solution where we can update it by manually getting OAuth Token from single appscript project but, Is there any way we can just mention sheetID and it updates project behind all those sheet all at once?
From the question
Is there any way we can just mention sheetID and it updates project behind all those sheet all at once?
There is no direct way to do this. Related issue Retrieving Project ID of Container-Bound script (created in 2018, as of the end of 2021 it still has the "New" status).
Assuming that by "SheetID" you mean the spreadsheet id, it's possible to use the Spreadsheet Service and the Drive Service to get a Class Spreadsheet or Class File object, respectively. Unfortunately those classes hasn't any method to retrieve the bounded Google Apps Script project.
The same for the Sheets API, Drive API and the Apps Script API.
The indirect way is to build a list of spreadsheets and their respective bounded scripts but it's clear that this is not what you are looking for. Anyway, build the list of spreadsheet/script projects pairs and store it somewhere (it might be an spreadsheet if the list is not humongous. CLAPS has a command to retrieve the list of the recent projects, and it's possible to get the container id from the script project metadata (see https://developers.google.com/apps-script/api/reference/rest/v1/projects/get)
Considering the above, the advice to make it easier to maintain your scripts "for the next time" is to either use a library or to create and add-on.
Resources
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet
https://developers.google.com/apps-script/reference/drive/file
https://developers.google.com/apps-script/api/reference/rest/v1/projects#Project
Related
How to programmatically create a list of Google Apps Script projects owned by me?
List Google app script projects
Google Apps Script: Get the ID of script bound to a spreadsheet
How to make Google Apps Script library always serve the latest version
If you're asking can you reference different codes from the same document but not the same sheet in a new code, the answer is yes. But, there is no way to manipulate this code other than editing the original code. I would recommend manually getting an OAuth Token from a single Apps Script Rroject.
A different way to control those Spreadsheets can be with a NodeJS project locally, using Clasp and Multi-clasp2 (https://www.npmjs.com/package/multi-clasp2).
I copied a spreadsheet to be used by multiple people with script attached. My question is how do I modify script in the project without having to copy and paste each change to .gs files in the project?
This is the base document from which the other versions were copied:
Create a central script and then publish it as a library. Have your template linked to the library. Make sure that the template is using the "dev" version of the library so that any future changes will be reflected for everyone.
There ARE ways to manipulate the GS files using clasp but it is much less straightforward than using libraries and overkill for this use case.
https://developers.google.com/apps-script/guides/libraries
Yes, it's possible to "upload" a .gs file to an existing Google Apps Script project. To do this you might use CLASP, GAS GitHub Assistant (a Chrome extension) or directly the Google Apps Script API.
Please bear in mind that you also could alleviate these pains (having to update the bounded to spreadsheet GAS project copies) by taking a different approach. You could use libraries or to create a Workspace Editor add-on.
Libraries help by keeping a big part of your code in a single place but you might still have to update the copies i.e. if you are using simple triggers like onOpen and onEdit or if you change library function name, add a new function, etc.
Add-ons help by keeping all the code in a single place bu you have to publish the add-on to the Google Workspace Marketplace. If you have a Google Workspace account the add-on can be published for internal use.
I have a google sheets which is linked to a form and a google scripts that was created in the menu of google sheets. This script runs fine and I use it to format the data. However, I have another script which I created from the google developer console, which sends and receives data from a website. I need this second script to get the value of a cell in the google sheets.
How can I link this second form to the google sheets while keeping the script already linked to the sheets?
When looking at responses to other questions they seem to be about individual sheets in a set so just to clarify, when referring to google sheets I mean the whole google sheets document. I only have one sheet anyways.
If it helps: The script I want to add acts a bit like a server and is being deployed by google scripts while the script that is already linked is only run when I call it on the sheets.
There is not way of attaching a standalone script to a document making it a bound script (which is the actual terminology for the script linked the the document).
The simplest way would be to copy-paste the code to the other script. You can deploy the bounded script.
If you really need more than one project for whatever reason, you can enable the Apps Script API and use projects.create (read reference) to create another one. If you don't know what this means, you probably shouldn't use it as it's finicky at best.
I found a solution which was to link the script to the google sheets using
var ss = SpreadsheetApp.openById("SHEET_ID_HERE");
I can then use ss.getActiveSheet().getRange().getValue();
and other funtions.
Thanks for the help!
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.
What I want to do
Copy a google sheet programmatically via API.
Change the copied sheet's cloud platform project to the original sheets project, when copied Google creates a new google platform project, that's not what I want.
Get the script id of the copied sheet.
All of the above only programmatically, I already know how to do these things manually by clicking menu's.
What I have already tried:
This google documentation article tells me how to change a platform project manually, I want to do the same thing via some API that Google may be providing.
You can copy a sheet programmatically via this API, however you can't get the associated script id or the project and it doesn't let me set the cloud platform project.
I know how to get the script id from within the sheet, I want to do this from outside the sheet.
Note that the API's I need have to be accessible via outside the sheet, via a program that I host myself.
I realized what I was really looking for were Add on's. Via that I can keep the apps script project as one which solves all my requirements.
Read more about App Script addons from this google doc page: https://developers.google.com/apps-script/add-ons/