Call a function with an argument in Google app's newTrigger("myFunction") - google-apps-script

I am currently trying to call a function with an argument in Google app's newTrigger() function.
I have a function called writeToSheet(sheet). Where as I enter the sheet name into the argument and the custom output is written to said sheet. It all works fine, but I would like to pass this into a time driven trigger with different sheets being written to at different times.
So far, entering ScriptApp.newTrigger('writeToSheet("Production Sheet")') within Google's trigger building example doesn't work. How would you go about solving this problem?

So far, I have had to create a function that executes what I need.
function prodSheetUpdate() {
writeToSheets("Production Sheet");
}
Then pass that function in my trigger.
ScriptApp.newTrigger("ProdSheetUpdate")
...
...
It doesn't seem like you can pass an argument in a trigger. If anybody knows otherwise, I am still curious!

you must pass a string to newTrigger
ScriptApp.newTrigger('writeToSheet('+'Production Sheet'+')')
or
var mystring
ScriptApp.newTrigger('writeToSheet('+mystring+')')

Related

How to fix error in google sheet custom script [duplicate]

I have a .tsv file from a tool and I have to import it the Google Sheet (nearly) real-time for reports. This is my code for importing:
function importBigTSV(url) {return Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText(),'\t');}
It worked till some days ago when Error messages keep saying "Exceeded maximum execution time (line 0)."
Could anyone help? Thank you a lot!
Issue:
As #TheMaster said, custom functions have a hard limit of 30 seconds, which your function is most probably reaching. Regular Apps Script executions have a much more generous time limit (6 or 30 minutes, depending on your account), so you should modify your function accordingly.
Differences between functions:
In order to transform your function, you have to take into account these basic differences:
You cannot pass parameters to a function called by a Menu or a button. Because of this, you have to find another way to specify the URL to fetch.
Values returned by a regular function don't get automatically written to the sheet. You have to use a writing method (like setValues, or appendRow) to do that.
A non-custom function is not called in any particular cell, so you have to specify where do you want to write the values to.
Since, from what I understand, you are always fetching the same URL, you can specify that URL just by hardcoding it into your function.
Solution:
The function below, for example, will write the parsed output to the range that is currently selected (at the moment of triggering the function). You could as well provide a default range to write the output to, using getRange:
function importBigTSV() {
var url = "{url-to-fetch}";
var range = SpreadsheetApp.getActiveRange();
try {
var output = Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText(),'\t');
var outputRange = range.offset(0, 0, output.length, output[0].length);
outputRange.setValues(output);
} catch(err) {
console.log(err);
}
}
If the URL can change, I'd suggest you to have a list of URLs to fetch, and, before triggering the function, select the desired URL, and use getActiveRange in order to get this URL.
Attaching function to Menu:
In any case, once you have written your function, you have to attach this function somehow, so that it can be trigged from the sheet itself. You can either create a custom menu, or insert and image or drawing, and attach the script to it. The referenced links provide clear and concise steps to achieve this.
Reference:
Custom Functions > Return values
Custom Menus in G Suite

Call custom function in google sheets only once and save value to cell

I have written a custom google apps script function that is being called by a google sheet. In this function an API is called and the result of this API is being returned so that it gets displayed in the calling cell of the google sheet.
My problem now is that the sheet calls this function everytime I open the document and there are over 80.000 cells with this function.
Is it possible to save the returned value to the cell and don't call the custom function again when an value has been returned? I want the function being called only once per cell, even if I close the document and reopen it. The returned value should be saved until something else is being written into to cell. That would make my sheets document much more usable than the current state.
From the question
Is it possible to save the returned value to the cell and don't call the custom function again when an value has been returned? I want the function being called only once per cell, even if I close the document and reopen it. The returned value should be saved until something else is being written into to cell. That would make my sheets document much more usable than the current state.
By solely using the custom function it's not possible. There isn't a straight forward solution is to achieve this, in order to keep things simple you should look for another way to implement what is being done by the custom funtion.
One option is to use a trigger (including a custom menu / a function assined to a drawing) to check if the cell that should have the value returned by the custom function is empty and in such case fill it with the corresponding value. The best trigger to use will depend on your spreadsheet workflow.
As specified by Ruben this is not possible, with a custom function.
In my particular case I have resorted to using an Apps Script function that is triggered by an edit event of the spreadsheet and verifies if the event is in the column where the function that I want to execute only once should be, later replacement its content with the result of calling the API.
function freezeValue(e) {
var rangeEvent = e.range;
var col = rangeEvent.getColumnIndex();
if (col === 2) { #Verify column of event
var value = rangeEvent.getValue();
/*Call your api*/
result_api = CALL_API(value)
rangeEvent.setValue(result_api);
}
}
Keep in mind that this implementation only works when each of the cells is edited one by one. To do the same with a row or a complete array of elements, you must go through each of the cells and follow the same procedure.

Call function from cell value, then run function

I'm looking to call a function from a cell value, then run that function.
In the script below, A45 in the active sheet contains the function I want to run.
The text in A45 is "RunScript1()", however this will change depending on the sheet being used.
function GetScript(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var cell1 = sheet.getRange('A45').getValue();
cell1 //// help here please ////
}
Thanks in advance.
Change the text to function name without brackets: RunScript. Then you can use
this[cell1]();
this refers to the global namespace and contains the function's names. Therefore the function can be accessed and executed.
Although limited to the function name, Note that this exposes your account to arbitrary code to be run from the sheet. If you shared the sheet, any one with evil intentions may be able to trick you into executing arbitrary code, like deleting your gmail or sending email as you. Therefore, this method should preferably be avoided. Use custom menus or a pre written onEdit script instead.

Google Form Submission Values are Undefined

I'm trying to capture some values from a form submission in a function and do some processing. So far, I'm having trouble just capturing values.
I have a very simple form set up with one field. I have the following code to pull the values and put them onto the Log.
function myFunction(e) {
var val1 = e.values;
Logger.log(val1);
}
I have set up the appropriate triggers such that the script runs whenever I submit a form, however, I always end up logging undefined.
[14-10-27 21:58:16:451 EDT] undefined
I've done this before, so I'm wondering if I'm missing a step or if something is wrong with my setup.
Thanks.
You need to have the function:
function onFormSubmit(e)

How to Use plugin RowEditing to Updating values

I have a Grid panel and RowEditing Plugin. I search how to use events when i click on Update Button.
I can display alert() but i don't know how to retrieve updates datas ?!!
${prefix}grid.on('validateedit',onEdit, this);
function onEdit(e) {
alert('UPDATE');
};
I have just one line on my Gridpanel. I need to retrieve all data to updating it :)!
Thanks :)
In your onEdit function, you receive the edit object (e), and you can access everything you need to from here.
For example, if I want to access the store... I can do so from within the onEdit function like
var store = e.grid.store;