How do I use a google apps script in another spreadsheet? - google-apps-script

I made a lot of functions to use in google docs spreadsheet.
I also saved this script as version 1.
I also made a deployment and it got installed as an add-on. But this does not appear in Add-ons.
I also made a new script file not associated with a spreadsheet and pasted in same code.
I can import it as a library into another spreadsheet and use functions in script, but I can not use functions in spreadsheet cells.
eg. if library has a function pc_rand(), I want to put '=pc_rand()' into a cell. Or, '=lib.pc_rand()'
How do I to use this script in another google docs spreadsheet?

I believe your goal as follows.
You want to obtain the same result of the script for Spreadsheet "A" at the Spreadsheet "B".
In order to achieve your goal, I would like to suggest to use a Google Apps Script library. When the script of Spreadsheet "A" is used as the Google Apps Script library, the same result with the Spreadsheet "A" can be obtained at the Spreadsheet "B" by calling the script from Spreadsheet "B".
But, from your replying, there is an important point for this. When you want to use the functions in Spreadsheet "A" for the Spreadsheet "B" as the custom functions, unfortunately, in the current stage, =pchatalib2.pc_rand() cannot be directly used. (In this case, it supposes that pchatalib2 and pc_rand() are the library identification and the function of the library, respectively.) So, in this case, it is required to create a function in the script editor of Spreadsheet "B" as follows.
const pc_rand = () => pchatalib2.pc_rand();
By this, when =pc_rand() is put in a cell, the function of Spreadsheet "A" can be run at the Spreadsheet "B".
References:
Libraries
Custom Functions in Google Sheets

Related

Can I use Google Apps Script to activate my spreadsheet?

I have an active spreadsheet, I want to select a cell in this spreadsheet and then bring me to another Spreadsheet. I used app.openByUrl to open the spreadsheet and then get my wanted sheet by name and then activate the cell by getRange, the script is finished but it didn't take me from the active spreadsheet to the other spreadsheet, the cursor remains in the cell of the active Spreadsheet. I use the following code. Does anyone has an idea?
app.openByUrl("https://docs.google.com/spreadsheets/d/1S9Kc6foLZ_bXqs4/edit#gid=502179704").getSheetByName("Verification Candidates").getRange('D4:F4').activate();
It's not possible to activate an external spreadsheet by solely using Google Apps Script as
Class Spreadsheet doesn't include like similar to Class Range activate
The Class Spreadsheet "open" methods open the spreadsheet on the server side, not on client side.
You might be able to do what you are looking by using other tools (i.e. a complex bookmarklet)

How to create a new Spreadsheet with a connected script within an App Script?

Within a Standalone App ScriptI'm creating a new spread sheet using the following line:
var new_ss = SpreadsheetApp.create("File_Name");
Now I wanna link a predefined App Script to this spreadsheet. How to do this? I couldn't find any method within the Documentation or by searching Google
I found the ScriptApp, but it doesn't provide any methods. I also found the API, but i don't think its available within App Script
I was looking for the same answer and bacuase I haven't find it, I change my approach. I created a script bound to a Google Sheet. When a new Google Sheet is needed I make a copy of the "template" Google Sheet with the bound script. I have a sheet name Parameters so the users can control the bound script.

Google Script reading from a dynamic Google Sheet

I have a Google Script that reads from a Google Sheet. The only content of the Google Sheet is an importData() function that imports a CSV file. Will the importData() function in the Google Sheet be called every time the Google Script accesses the data in the Google Sheet? Or does the importData() function only get called when the Google Sheet is opened manually?
importData() is a formula, that is evaluated and reevaulated when a user opens the spreadsheet and when other cells that are referenced by the formula change.
So, this means the output of the formula is not available for Apps Scripts.
However, using Apps Script you can easily write the code to do what the formula does.
See this question for how to parse CSV data

programmatically edit script in Google Drive document

I have a Google Drive document which writes values to a Google Drive spreadsheet using Google Apps Scripts.
The script associated with the document looks a lot like this:
// must change value to actual spreadsheet ID
RobProject.spreadsheetID = "spreadsheetID";
function onOpen()
{
// do stuff;
}
Each time I create a spreadsheet and its related documents, I manually change the value spreadsheetID to the spreadsheet's ID so the documents know to which spreadsheet they should write their values.
I would like a programmatic way to fill in the correct value for spreadsheetID into the Documents' scripts.
When I search for "edit scripts programmatically," I get tutorials for creating Google Apps Scripts, not editing scripts with scripts. Is there any way to edit Google Apps Scripts with a Google Apps Script?
If I understand correctly, you are working with a document and a spreadsheet. The document needs to know the Id of the spreadsheet.
There are some ways to access the Google Apps Script code through the API, but that is only for standalone projects, not for container-bound scripts (unfortunately).
You could consider using a naming convention for the document and spreadsheet so that you could use the Drive service to get from the document to the spreadsheet (DriveApp.getFilesByName()). Or possibly organize them by folder (DriveApp.getFoldersByName(), folder.getFiles()).
If you wanted to store the spreadsheet Id in a project property, you could build a UI in the document that let the user open up the list of files in Drive and pick the associated spreadsheet and then store the Id (ScriptProperties.setProperty('SpreadsheetId')).
Don't forget that onOpen has a parameter. You could use following code:
// Define global variable somewhere
RobProject = {};
function onOpen(e) {
RobProject.sSheet = e.source; // maybe the spreadsheet object is as useful as the ID
RobProject.spreadsheetID = e.source.getId();
// do stuff;
}
Please, for your own sake, don't try to write selfmodifying code.

Google Apps Script - how to update scripts attached to many spreadsheets?

I wrote a script for a spreadsheet template. My user makes copies from the spreadsheet template and the attached script. There are many such instances both spreadsheet and the script. When I update the script for the spreadsheet template, I(or my user) have to manually update all scripts attached to spreadsheets. All scripts are the same. Is there a way to synchronize the script update automatically?
What you can do is maintain a single standalone script and publish it as a library instead of having it reside in the spreadsheet template. This way, you have to change the script in only one place.
To work with triggers such as onOpen, onEdit etc. you can have shell functions in your spreadsheet template. Something like
function onOpen(e){
LibraryName.onOpen(e);
}