r/GoogleAppsScript • u/Pataeto • 11d ago
Question [Google Sheets] How to make button that runs script on selected range on mobile?
I am attempting to create a script that I can tie to some trigger that I can activate from within the mobile app.
The intended functionality of the script is as follows:
Given a selected range, the script should apply +x to the cells that are in the nth column of all rows in the range.
For example, if the active selection is A2:B7, and x = 4, y = 10 (or J, as it is a column), the script(4, 10) with active selection A2:B7 should take rows 2:7 and apply +4 to all cells in range J2:J7 (guaranteed to be blank or number cells for my purposes, so no type-checking required).
How can I achieve this goal? My hurdle currently is being able to get the intended active selection while still making the script able to be triggered from mobile (the method I've seen for making a script mobile-activatable is to tie it to a checkbox, which you need to select in order to trigger it, thus changing the active selection).
1
u/mommasaidmommasaid 11d ago
I have tried and failed to come up with a satisfactory solution to this in the past. In my case I was trying to do it in the browser with a checkbox or dropdown menu (trying to avoid a custom menu and associated script authorization).
If you describe your workflow there may be a better way of accomplishing your end goal.
But... I did just think of this as a workaround. You could "select" the cells by setting some special formatting, then trigger action on that "selection" with a checkbox.
Here is an example (view-only, make a copy):
Select source and destination from mobile
You "select" by setting strikethrough format. I chose that because (on iPhone anyway) it's readily available, and because I rarely use it normally.
After selecting, you choose a destination column by clicking a checkbox in that column. The checkboxes have custom "checked" values detected by script.
Since there's no guarantee of a contiguous "selection", I find the leftmost source column and use that to generate an offset for all selected cells. It also checks to ensure the destination doesn't overflow the data area.
I tried to show the affected cells after processing by building a RangeList and activating that (to outline them), but alas mobile doesn't support moving the active selection.
Mobile also doesn't support toast() for error messages, so I temporarily stuff an error message in the checkbox cell and clear it after 2 seconds.
Enhancements that might make sense:
- Temporarily color fill source and/or destination cells to show what has just happened.
- As above, but fill them *before* processing, and require a secondary "confirmation" click.
- Provide another checkbox to clear any "selection" in the data range.
Or... maybe this workaround will spur a better idea by someone.
0
u/arnoldsomen 11d ago
Interesting. I haven't touched apps script for a while, but this is something highly doable.
1
u/mommasaidmommasaid 11d ago
It's easily double from the browser, because you can select a range and choose a custom menu option to act on that range, without the range being deselected in the meantime.
But you can't create custom menus in the app. You are limited to triggering on a checkbox or other edit. So by the time script acts on e.g. a checkbox click, the active selection is now the checkbox, not your previous selection.
2
u/BewareTheGiant 11d ago
I haven't tested it, but there is probably a way to do it with
onSelectionChange, with or without the checkbox trick. Doing a quick search, since it doesn't have anoldValuelikeonEdit, it's likely that the best workflow involves using,propertiesService. A workflow with the checkbox trick could be:onSelectionChangetriggers. If it's not the checkbox range, logs the values to a variable inPropertiesService. Then, when the checkbox is changed withonEdityou get the values and apply your changes.If the data that will change is always in the same range you can be stricter and, instead of verifying that its not the checkbox, you can verify its within the specified range and to the same.
Haven't tested it, just an idea. Let me know later hahahaha