Google Sheets - Track Current Cell Selection? - google-apps-script

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

Related

Google Sheets Hyperlink to Apps Script in an IF statement

Well, I may be being a bit too tricky here.
I have a text in column B, where if it starts with 'I', I want a hyperlink. If it starts with anything else, I want the result of some time calculation.
The time calculation using the ArrayFormula() is fine, no need to fix that.
The problem is the Hyperlink is showing the text (it is not showing as a hyperlink) "Create next week activities" but the CreateNextWeek() function is not firing.
This is the formula in my cell.
=if(left($B22,1)="I",HYPERLINK(CreateNextWeek(G22),"Create next week activities."),ArrayFormula(if(len(F22),F22+if(len(Q22),Q22,N22)/24,)))
In case anyone need my app script code, it is simply this:
function CreateNextWeek(originDate) {
var ss = SpreadsheetApp.getActive();
var title = 'Function Started';
var message='Create Next Week for after ' & originDate;
ss.toast(message,title);
}
I click on the cell, nothing happens.
I think it has something to do with the cell needing to change from text mode to hyperlink mode and back again depending on my IF condition. Is that a possibility? Not sure how to fix that either, if it is the case.
Soo, how do I make this cell behave the way I want it to work?
Ok, so the answer appears clear.
HYPERLINK will not fire an Apps Script function.
You cannot create a HYPERLINK within an IF() structure. It will appear as a text. In saying that, it is possible to use an IF() inside a Hyperlink().
Using a CheckBox combined with an OnEdit() action is a very clever way to activate an Apps Script.
You cannot simply hide and/or unhide a Checkbox simply with an IF(). The best you can do is hide/unhide a column using some Apps Script.
My end result is to create a button using the drawing tools and add a CreateNewWeek() function. Whilst I can't pick a week using the date in the CurrentRow, I will look down a column for the last ISO week number and build my new week based on that value.
For me, sacrificing a date functionality to keep a one only button (place to fire my Apps Script) is a great compromise.

Edit cell above cell that called a function?

I'm using Google Apps Script to make a function that will be called in a Google Sheet. The function does some auto calculating for a spell effect. Some spells need the player to provide extra info (How many people are you shielding, etc) and some don't need any additional inputs. If extra info is needed, the user provides it in the cell directly above the cell that calls the function. I'll call this the "input cell" for clarity. The "function cell" is the one that calls the function, directly below the input cell.
My question is this: if the extra info is unneeded, as determined by conditionals within the function, how do I overwrite the contents of the input cell with '-'? I can't put a formula within that cell directly, because the user would overwrite the formula when entering the additional information. The function also returns a value to the cell that called it.
I've tried a bunch of techniques but I can't figure out how to do it.
I tried returning an array of both values, but it flows downwards instead
of upwards, and I couldn't find anything for "return array offset
upwards" or something to that effect.
I can't use absolute references
like "L17" because the function can be called from many places on the
sheet, and it also only needs to mess with the cell directly above it.
using setValue() or setValues() threw a "no permission" error.
I would attach my code, but it's a mess of half-deleted attempts that didn't work, and wouldn't be much help at all. I also don't want to ask anything too specific, for fear of running into the https://xyproblem.info problem. I just want to be able to have a function edit the cell directly above it, and I don't care about which technique gets that accomplished.
Any help is greatly appreciated. Thank you!
No cell can contain both a formula(or a output of a formula) and simultaneously contain a user entered value. That was always the case with any spreadsheet, Excel or Google sheets or any other. It's one or the other. Choose one and redraw your logic!
For your specific problem, if you can reflow your logic, you might try simple triggers like onEdit.

Activating a new cell not working within Google Sheets onSelection(e) execution?

Is there some way to force the selection of another cell (e.g. A1) after someone has selected a different cell (e.g. B2)? If i use a simple ...getRange(1,1).activate() code, it works when I hit "Run" in the script editor, but not when a selection is made in the real spreadsheet. Please help?
I've basically written a long "onSelection" code that allows someone to mark their answers to a trivia game as "Correct" or "Wrong" by clicking on the corresponding cells. (I do this with onSelection(e) because buttons don't work on mobile.)
However, if they get two answers correct in a row, onSelection(e) doesn't work because they've already selected "Correct" the first time. I want the code to select 'A1' automatically so it's ready for their next Correct/Wrong input.
A code as simple as the following works when I select any cell in Google Sheets and then hit "Run" in the script editor, but it doesn't not work when I select a cell with my mouse.
function onSelectionChange(e){
var sheet = SpreadsheetApp.getActiveSheet()
sheet.getRange(1,1).activate();
}

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 scripts unhideRows not working

I have a script for Google Sheets I am working on. I realize there are better ways to do what I am attempting (cough html/database cough), but I am required to make this spreadsheet work.
In short, I am trying to hide and unhide rows dynamically. There does not appear to be a way to get filters to update without redoing them, so I am attempting to use hideRows to hide them and unhideRows to reveal them as needed.
The hideRows command works below. The unhideRows command does not. At this time, they are literally this close together in the code. Originally, I was hiding on one sheet and unhiding on another, but set up like this to troubleshoot. There is no filtering on this sheet (because it didn't work, I turned it off). I tried setting the value in unhideRows directly to the value tested instead of a variable.
The row in question hides, but does not unhide.
I tried unhiding a different row just in case google was fumbling with hide this, unhide it back to back. I am not getting unhide to work.
{ //thisRow = the row number of a range, in this case 2 if output to screen
pImages.hideRows(thisRow);
pImages.unhideRows(thisRow);
}
Apparently, the answer is not to unhideRows() which is the one the documents alluded to, but showRows () which does make more sense in everyday language. Thanks again to Mr.Houdini on the Google forums.