Adding script to a button which can display Time now - google-apps-script

Hi I am trying to make a spreadsheet with Button formed through shapes (using Drawing tool) and then assign script to it, to track Start time and End time. Basically the two button side by side would say Start and Stop, when you start working, you click the button and the start time would get recorded and when you click the Stop button it would record the time then as Stop time. I was reading the script tutorial and it said that now() argument is not allowed, is there any other way to do this on Google Sheets, because in Excel it is pretty simple.

Google Apps Script is based on JavaScript : new Date() is the equivalent of now()
Your idea is not bad but these button are not fixed, they move with the page whenever you scroll down...
I'd suggest using the sidebar available in new (standard) version of spreadsheets to place your 2 buttons and the corresponding results.
You can use HTMLService or UiApp to build a pretty user interface in the sidebar.

Related

AppsScript with Google Sheets: Conditionally load images based on user interaction/when a range is selected

We've got a google sheet with user editable text that will eventually be used within images. We have a web application that generates the images and serves them, so they can be embedded within the IMAGE function provided within Sheets.
This works fine and ends up being something like:
=IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3) where 'previewText' is simply formatting the string to pass to the web tool.
However, to avoid overloading the server and generating a ton of requests every single time someone loads the sheet, I'd like to make it so images only load in some condition (i.e. a user presses a button or they highlight the text), is there some way of doing this?
Add a checkbox to E2:
=IF(E2,IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3),"Check to preview")
You can also use Apps Script for this!
You will have to start by:
Go to Tools > Create a new script;
Use the following script:
function loadImages() {
let sheet = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("SHEET_NAME");
sheet.getRange("CELL_YOU_WANT_TO_PLACE_THE_FORMULA").setFormula("YOUR_FORMULA");
}
Do not forget to adjust this script such that it meets your needs accordingly.
Insert a new drawing into the sheet;
Attach a script to it by going to the three dots symbol on the drawing; when asked about the name, input loadImages which is the function which we had created previously.
Press the button on the sheet and that's it!
Reference
setFormula(formula).

Double clicking Google Sheet Drawing with attached script opens Drawing editor - how to avoid?

I'm using a Drawing as a way to call a script. My use case expects this button to be pressed in rapid succession sometimes (two or three times at once).
I simply want the button to call the script multiple times (it does) without opening the edit drawing dialog.
Are there permissions I can set to prevent the user from opening this dialog?
Or is there a way to lock the drawing?
Or is there a different way to create a button that can be used in this way? (I looked at onSelectionChange(e) but this doesn't solve my multiple click need.)
Thank you.
A simple solution is to not use a drawing at all. Here's how:
Make a nice snip of the UP arrow, and copy it to the clipboard
Select the cell where the arrow is now and delete the Drawing.
In the same cell paste the snip from the clipboard
Right click on the pasted Arrow image and assign the script to it.
Notice that you can click many times and it will work as you expect.
Repeat for the DOWN arrow.

How can a calendar refresh/reload after a event was changed?

I'm changing a event using the CalendarApp script, but it seems it appears on the Calendar UI only after a random time, ranging from seconds to minutes. Is there a way to force the event to be updated/refresh on the Calendar immediately after it was changed on the script?
I tried using several combinations of
cal.setHidden(true);
cal.setHidden(false);
cal.setSelected(false);
cal.setSelected(true);
CalendarApp.setHidden(true);
CalendarApp.setHidden(false);
CalendarApp.setSelected(false);
CalendarApp.setSelected(true);
but I still have inconcistent results. Any idea?
Thanks in advance.
The calendar service and the calendar Browser Ui are two separate things, they have no relationship except being linked to the same source (the calendar itself), there is no way for a script to interact with the calendar Ui... You can use the refresh button under "more" menu in the upper right corner of Calendar Ui but that is all you can do.

How to call a script in Google Spreadsheet above a frozen row?

Above a frozen row I want to put an image that is linked to a script; e.g. click the image and a script runs.
I can insert an image and add a script to it, problem: can only be
done under frozen row.
I can insert an image using =image("imageurl";2), I can even put a
hyperlink to this =hyperlink("url"; image("imageurl";2)) and this
works nicely but I can't call a script.
Any ideas on how to solve this?
How do you add UI inside cells in a google spreadsheet using app script? ... shows how to insert a drawing and link it to a script. Unfortunately, as you've found, floating images are not allowed in frozen rows or columns, so that technique breaks down if you must freeze rows. A quick search on the interwebs will show that this has been a long-term complaint about Google Sheets. (For instance.)
There are options, none of them ideal:
Use a custom menu function instead. That means users need to hunt a bit to find the menu option to kick your script, but it does stay in view no matter where they are.
Use the hyperlink & image work-around you've described, but use a published script url as the target. That will allow you to run a google apps script, indirectly.
Use a sidebar. OK, this isn't actually supported in Sheets yet - visit Issue 3162 and star it for updates.

Linking one cell to another in a spreadsheet

I have a dynamically generated spreadsheet. I would like to be able to allow the viewer to go to a different sheet/cell when a cell is clicked on. I'm having trouble figuring out a good way to do this.
Going to the new cell is fairly easy: Spreadhseet.setActiveSheet()/Spreadsheet.setActiveRange() work fine. My thought was to encode the link destination in the contents of the source cell, and then write a script to "follow" the link.
I tried using images or drawings. But there were two problems with this: while images can be inserted programmatically, I did not see a way to programmatically associate a script with them. And the called script does not have an associated event tied to it, so there is no way to see what cell the user clicked (clicking an image does not set the active cell).
I thought about using onEdit(). This almost works, but onEdit is only triggered on changes (not selection) so the user actually has to modify the cell in order to "link". When testing I tended to zap the contents of the cell by accident with onEdit.
I tried making a menu item, and in the menu's script get the active cell's contents to determine where to go to. This worked but was a bit more difficult for the user than I was hoping for.
I was hoping someone would have a creative solution to this problem.
Thanks!
From what you are describing the drawing or image insertion should work since it has exactly the same functionality as the menu item. You can assign a script to it just like in a menu.