How to know which element called the callback function? - google-apps-script

I have a script creating buttons in a table.
Each button is assigned to the same callback server function.
My question is how do I know which of these buttons triggered the server call?
Also is there a way of outputting a msgbox from the server script? When I try browser.msgBox, it gives an error saying that msgbox can't be used in this context.
var clientDel = app.createServerHandler("delStudent").addCallbackElement(grid);
table.setWidget(t, 4, app.createButton("Delete", clientDel).setId("del1"));
function delStudent(e){
var app = UiApp.getActiveApplication();
e.
return app;
}
Thanks,
C Singh.

simply e.parameter.source will return the ID of the widget that originates the handler function
You can use a label or a textBox that you create in your UI and set invisible. Then at any moment you can use getElementById(label_ID').setVisible(true) and put any text in it.
If you want it to appear at a specific position above all other widgets you can do that quite simply using setStyleAttributes, see this other post(server version) for further info and practical example

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

Cognos Report Studio List Prompt HTML Item

I am working on a dashboard in Report Studio with list prompts that will load different reports. I have a single list prompt and an HTML button that will launch the report selected from the list. I am trying to add additional list prompts for different report categories. The issue is that all of the HTML buttons will only look at one of the list prompts. The excerpt from the script is as follows. I change the .getControlByName() to reference my different list names, but that doesn't help.
<script>
function open_win()
var oCR = cognos.Report.getReport();
var myPrompt = oCR.prompt.getControlByName("classlist");
var v = myPrompt.getValues();
var selectedValue =v[0]['use'];
window.open(selectedValue);
return true;
I am not familiar with java script, so I am guessing at what these commands do.
Each prompt needs its own Name property populated. Assuming you've done this is what the code would look like:
function open_win(section) {
var oCR = cognos.Report.getReport();
var value = oCR.prompt.getControlByName(section).getValues()[0].use;
window.open(value);
}
For this to work, you'd have one HTML button for each section.
In each HTML button's onClick definition, call the universal open_win() function, passing in the name of the associated section which needs to match the associated prompt: e.g. open_win('classlist').

getTag() method not properly working for google apps script

What i am doing in writing a script that lets the User interact with a data table. Every series that the user chooses creates a button, and then plots the series on a graph. if the user click the button it rooms the series. All there the data is stored in a hidden JSON string. the columns, or series that the user whats to see are stored in an array that i call index, it is also a hidden JSON string) Each button is connected to its own client handler, which has a
.forTargets(the index i was talking about).setTag(to the corresponding column in the data array)
and they are all connected to the same server handler. So when the button is clicked the client handler sets the tag for the index to the series that it is supposed to be removed. Now the server handler will run it get the index by ID and get the Tag. This is were it goes wrong. The tag is always null.
The first thing i tried was to see if my client handler was not working properly. So i set the tag of the index to some number, but the getTag method in the Server handler still returned null.
idk, but maybe Tags are not saved in the UI instance??
Below is the first bit of the Server handler.
function clickServer(e) {
e = e.parameter;
var app = UiApp.getActiveApplication();
var master = JSON.parse(e.dataTable, "reviver");
var index = JSON.parse(e.index, "reviver");
var hidden = app.getElementById("hiddenIndex");
var tag = hidden.getTag();
I think the issue you are meeting is more related to timing : handlers are called simultaneously, this is true for client an server handlers as well, that means that if the client handler changes a hidden tag value this change happens too late for the server handler function to 'see' it. What you should do is create a separate button to trigger the server handler that the user would use after all the other parameters where set.
This very same method is used in the known workaround used to get radioButtons value
Also, why do you use tags on the hidden widget ? you could use it with direct access by setValue() and e.parameter.hiddenName since they are already invisible by nature... ?
Note also that you can set a value in client handlers as long a these values are defined inside the Ui instance (the do Get function) either by constant variables or by another client Handler in the same function, as shown in the before mentioned example with radioButtons... but that's only a detail.
In this context if you need to get a widget value (inside the doGet function) you should of course use getValue() to get the widget value as no e.parameter is available at this time.
I hope I've been clear enough, re-reading this I'm not very sure but.... just ask if something is missing ;-)
The tags values are passed to handlers via parameters. In this post this fact is explained in details.

automatically create google contact from my google form

I have a google form that my clients fill out. How do I have the form create a google contact automatically each time someone new fills out the survey? I dont want to have to copy paste each time just to create a new contact in my gmail
I'm trying to use this script but getting an error when I try to run it with some test data
function onFormSubmit(e) {
var timestamp = e.values[0];
var firstName= e.values[1];
var lastName= e.values[2];
var email= e.values[3];
var phone= e.values[4];
ContactsApp.createContact( timestamp , firstName , lastName , email , phone );
}
TypeError: Cannot read property "values" from undefined. (line 2
You will get that error message when attempting to run the function manually, as there is no event being passed to the function.
Disregard the remainder of the post (see Henrique's comment).
According to the documentation:
An event is passed to every event handler as the argument (e) . You
can add attributes to the (e) argument that further define how the
trigger works or that capture information about how the script was
triggered.
If you use the attributes, you must still set the trigger from the
Resources menu, as described in the sections above.
https://developers.google.com/apps-script/guide_events#TriggerAttributes
I can't explain why this is the case, but it seems you need to use the
simple event handler (ie naming the function onFormSubmit), as well as
applying an installable event handler to that same function. Then,
for me at least, it works as expected.
HTH, Adam
There's no "phone" argument in createContact function. Before attempting to create it from a more difficult to debug trigger, have you tried writing a simple function to create a simple contact just to check that you got it right?
Also, I remember an old error with on form submit parameters, that may be back. Anyway, it doesn't hurt to add a toString in all your parameters. e.g.
var email = e.values[3].toString(); //just to be sure

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;