Currently, I use onEdit and it is being called when I change values in a cell or range of cells, or when deleting, inserting rows/columns.
function onEdit(e) {
Browser.msgBox('edit: ' + (e.range && e.range.getA1Notation()));
}
The onEdit function is not being called when hiding or showing a row through the UI.
Is there a way to get my script to be notified of a hide/show row event?
Update: I wrote a feature request for this in google-apps-script-issues:
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4670
Update: As per Kriggs answer, the installable trigger onChange can be used
Here's how that works.
However, on the e (event) object, there's no range property from which to see what row/range was hidden or shown. Also Krigg's suggested Row method does not exist on e.source.
How can we leverage onChange to also get the row number(s) hidden or shown?
I just found out about onChange(), which handles specifically changes to structure in the Sheet, and aparently must be activated in the Trigger menu and then choosing a function. To know the ROW you can use just as onEdit(), e.source.Row();.
I tested a little bit and found it to be as erratic as time based and onEdit(), out of 3 consecutives hiding only 1 was Logged.
Related
I would be very grateful for some help.
I have a script that will hide a row in my Google Sheet if the value "1" is entered in a cell in column A.
It works as expected.
It does not work, however, when I set the values of non-selected cells. (I want to change these using a formula). I can see that my primitive script is only assessing the value of the current cell.
Instead, I want to constantly evaluate all of the cells in the range A34:A97.
Any row that has a value of 1 in col. A should be hidden.
Any row that has a value <>1 in col. A should be unhidden.
Can anyone help?
Thank you,
So you're probably using an onEdit(e) function and that only works with user edits. There is no trigger for edits that are performed by cell functions or other scripts.
Reference
How to Get the currently selected cell/range? was asked 3.5 years ago, but I'm hoping maybe some progress was made since then.
I'd like to make an interactive spreadsheet where there's a boolean grid that will display more information about the status of the currently selected cell in a static location, allowing users to quickly navigate using the arrow keys.
Referencing the previous instance of this question, I tried using the custom function below to get the cell address, although this isn't very dynamic.
function currentCell() {
return SpreadsheetApp.getActive().getActiveRange().getA1Notation();
}
When testing, this function returns the address of the cell the function is called from. I've tried pressing F5 while highlighting another cell and nothing happened. I've also dragged the formula across a selection and each cell only contains its own address (according to the documentation, it should contain the range).
I've also read Henrique's post on caching from 2012, and while I don't completely understand it, I get that we could not get scripts which update without edits being made or manual formula updates.
I would ideally like for the current cell to be tracked without any extra prompts from the user besides arrow key navigation or clicking to select a cell, but if pressing a non-altering key (like Enter) would allow for tracking, that would be fine. I also only need the selected cell, rather than a range, if that makes things easier.
Explicit Question: Is it possible to track the position of the currently selected cell in Google Sheets and store that position in a separate cell? If not, is there a way to do this without altering cell values?
EDIT: Adding an example for clarity. In this conceptual example, A2 =currentCell() as described above. If I were to hit the right arrow key, A2 would update to display I4, since it is now the current selection.
EDIT2: Jason Allshorn's response to Google app script monitor spreadsheet selected ranges appears to give a way of displaying the current selection (range) on the HTML input text. I'd like to do this, but store the value in a static cell on the sheet rather than input text. I'm new to scripting in Sheets and really only know what I've found for resolving this issue, so if I'm overlooking something, please let me know. I did look at Jason's example sheet and it wasn't clear to me what was actually happening there.
You will have to apply the technique suggested on How do I make a Sidebar display values from cells?. This consist on using using client-side code to constantly pulling values from the spreadsheet and then do the corresponding task.
One approach is to use a side-panel with an input field to set the cells to be tracked and another input field to set the destination cell.
You will have to use the onSelectionChange event in the app script to capture any selection change and then get the range name and put it in desired cell.
Something like the below:
function onSelectionChange(e) {
const range = e.range;
//Return in case of multi cell selection
if(range.getNumRows() !== 1 || range.getNumColumns() !== 1) return;
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A2").setValue(range.getA1Notation());
}
You can see the official documentation here
I have a Google Spreadsheet like this with a chart and a dropdown menu. I would like the data range of that chart to update based on a value selected from a dropdown menu.
I thought I might be able to add a Google App Script to the dropdown menu, but I can't find a way to add such script to anything but a drawing. Can someone tell me how to achieve this? Or point me into the right direction (Doc link?) as I'm new to both Google Spreadsheets and App Scripts.
Update:
From the comments it seems using App Scripts onEdit trigger might work.
The idea would be
to trigger onEdit when the value from the dropdown cell changes
fetch the cell value to use as an argument for another function
that in turn alters the data range of a chart.
As far as I can see onEdit triggers with changes made to any cell. Is there a way to limit this trigger to specific cells/ranges?
If I were you, I would use your drop-down menu as reference in a Query(). Often, I'll use a Query() in a hidden section of columns on my sheet, and use that hidden section as the data range for my charts. For example, if your drop down was in A1 you could have in B1 a Query() that uses a cell reference to find your data, and then hide columns B through however many you need to display your data. Then you make your data range B1:D (or whatever the range actually ended up being). Your example sheet has been moved to your trash, but if you share a data set with me, I could show you a pertinent example. Here is a less than pertinent example Ta-Da. I know that it is probably too general to use for your specific case, but perhaps if we knew more we could point you to a more specific work-around.
I'm managing a Google spreadsheet with a script. onEdit is working - I can colour a cell red by putting
SpreadsheetApp.getActiveRange().setBackgroundRGB(255, 0, 0);
in the onEdit function. I would like to put the same code in a onClick function, so that when a user clicks on a cell it immediately turns red. Is this possible? I can't find any way to do it in the documentation.
As stated on the comments above before there's no way (right now) to create an onClick() function for Google Spreadsheets. That makes sense due to the fact that the google app script functions are processed in the server, the amount of load generated by onClick() events execution would make pretty easy to deplete all the quota of requests provided by default.
You should be using the documentation for creating a button and clicking on it.
You can get closer....Create a validation for the cells with an option...like "Done". Click the dropdown, select Done. now create a trigger onChange or onEdit to fire. You can get a cell/range contextualized function....like firing on a row or column. This is easy to replicate and program for
You can add a Drawing as a button - https://developers.google.com/apps-script/guides/menus#clickable_images_and_drawings_in_google_sheets
I believe the new trigger onSelectionChange(e) will achieve this. It's in the docs but at time of writing isn't yet working on my sheet. https://developers.google.com/apps-script/guides/triggers?hl=en
Try this for a click event:
function onSelectionChange(e) {
e.source.toast('Sheet: ' + e.range.getSheet()(.getName() + ' Range: ' + e.range.getA1Notation());
}
Create a "Drawing" with background transparent; then move and place the drawing on top of the cell you need and then assign the script to the drawing.
in google apps script, how can I automatically run a script when I change the background color of a cell?
When I change the background color of a cell, the spreasheet is automatically saved and it says also when last edit was made, but if I try to use this event on an onEdit function to run a script, it doesn't recognize the event and does nothing. Thanks
onEdit is going to fire when any cell value changes, not with any formatting changes. You'll have to place the rules to change the bg colors in the script, assuming that the rules are based on cell values already in the sheet.
I'm afraid you'll have to manage this using a 'trick' as there is indeed no automatic trigger for background color changes.
Just a suggestion : If your spreadsheet is not too big you could simply memorize the whole color description in a string you previously stored in script properties and, based on a timer trigger, check if something has changed.
If this solution is acceptable for you (the timer trigger and not an immediate reaction) give it a try...
Use this var for example :
var backGroundString = SpreadsheetApp.getActiveSheet().getDataRange().getBackgrounds().toString()
and compare it to the same value stored in scriptProperties, if different, trigger your event...
I know it's a bit clunky but right now I see no other simple way to get what you want.
You could add a few conditions to make it work only at some moments or when the spreadsheet is open...