Using a Script to Hide a Row Dynamically in Google Sheets - google-apps-script

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

Related

Google Sheets - Track Current Cell Selection?

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

Changing date range for chart based on selected dropdown value

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.

Google spreadsheet trigger for hiding and showing row

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.

Determine the height of merged cells in Google Spreadsheet

I am creating a spreadsheet that will sum the daily inserted hours per activity for each week individually. This spreadsheet allows an increase in height for each week as needed
(ex. week 3 in image below demonstrates another row for inserting a 3rd activity per day).
What I would like to do is find a way to determine the height of the merged cells on the left to be used in dynamically determining the cells to include in a sum total in the last column.
I have found how to determine the existence of merged cells and how to determine the width of merged cells by exporting to HTML, but the latter seems it would not work for my situation as I am looking for a way to do instantaneous height determining to be used in an essentially fancy sum function.
I would like to know if/how it is possible to dynamically determine a merged cell's height using google apps script within google spreadsheet.
Any suggestions or guidance would be appreciated!
I'm assuming that you know where the first row of where the merged cells is. If you know the row position of the merged cell you want to change, I think you can use the getRowHeight() method of the Spreadsheet class, which returns an integer.
getRowHeight Google Documentation
If the problem is, that you can't find the row position of the merged cell you want to change, that's a different question I guess.
You can also set the height:
setRowHeight
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Sets the first row to a height of 200 pixels
sheet.setRowHeight(1, 200);

How to fix the function value in Google Spreadsheet?

How we can fix the result of the function in Google spreadsheets? For example, I have entered
=2*A1;
for B1 and what should I do to keep B1 fixed so that it won't be affected by the changes in A1.
The question can be awkward, but I couldn't have found a way to do it.
You can do it by replacing formula with its result:
Select cells, which you want to keep value only
Dropdown toolbar Edit, choose Copy
Dropdown toolbar Edit, choose Paste value only
I hope it's that what you mean
I think this might be the link you are looking for.
Simply write a "$" before the cell or column or whatever you wanna have to fixed.
Example: $D$4 if you write $D4 you will only get column fixed what I understand.
http://www.gcflearnfree.org/googlespreadsheets/14.3
The point of functions is so that B1 IS affected by A1. If you don't want a relationship there, don't use a function. As far as I know, you will need to just manually type in the value you want, or copy the cell, and right click the cell and click "Paste values only."