Can I make my UiApp draggable? - google-apps-script

Using Google Apps Script, I've made a UI for use on a Google Spreadsheet.
When called, it appears in the centre of the spreadsheet. I'd like to make it draggable so the user can move it out of the way if necessary - they may need to change cell selection whilst the UI is active.
Is this possible?

You should add a title, then you can drag it 'by the title' ;-)
example : app.setTitle("drag me here");

Related

Is there any way to change a Google Slides presentation without reloading it?

I'm basically trying to build a game board. For simplicity's sake, imagine a Jeopardy! board. I'd like to be able to click on a square (imagine it says $200) and go to the question set for that square. Then, when I return to the main board, I'd like that square that I clicked on ($200) to be blank, so I know I've already clicked it.
I want to be able to do this in a full screen presentation, without having to reload the slideshow at all.
I'm thinking, hoping, I could do a script that would just straight-up replace the image of a screen with text on it, to an image of a blank screen, but I'd also be okay with building the slide with two layers of images - one with text above one without - and deleting the top layer when it's clicked.
Click on and it becomes this:
Replacing the image via script can be easily done by using the following script:
function myFunction() {
var image = SlidesApp.getActivePresentation().getSlides()[0].getImages()[0];
// Get the Drive image file with the given ID.
var driveImage = DriveApp.getFileById('File ID of the new image');
image.replace(driveImage);
}
But unfortunately, assigning an image to act as a button to run script is only available on Google Spreadsheet. And by further checking it seems somebody already submitted a Feature Request for such a feature to be implemented on Google Slides as well.
References:
https://developers.google.com/apps-script/reference/slides/image#replace(BlobSource)

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).

Google Slides & Apps Script: Unlink all embedded sheet charts

I'm publishing Google Slides containing a lot of embedded charts coming from a Spreadsheet.
I would like to unlink the embedded charts (and thus avoid to get the update button when the data are updated in the spreadsheet).
If google proposes to update all elements at once through the "Linked objects" entry of the Tools menu, there is no option to unlink all in one shot. I would need to go on each chart and select unlink.
So I'm looking now the option of writing a Google Apps Script to do that without success.
I found a similar question on stackoverflow here:
Remove all hyperlinks of a Google Slide using GAS
But the removeLink function does not have any effect on my chart. I still see the chained icon on the top right corner.
Any idea ?
Unfortunately, it seems that in the current stage, there are no methods for directly removing the link to Spreadsheet from Speadsheet. But when the link to Spreadsheet from Speadsheet chart is removed, it is found that the object becomes an image. I thought that this might be used for achieving your goal. So, from this situation, as a workaround, I would like to propose the following sample script.
Sample script:
This sample script converts the Spreadsheet chart to an image on the 1st slide. By this flow, the link to Spreadsheet from Speadsheet chart is removed.
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const charts = slide.getSheetsCharts();
const chart = charts[0];
slide.insertImage(chart.asImage().getBlob(), chart.getLeft(), chart.getTop(), chart.getWidth(), chart.getHeight());
chart.remove();
The flow of this script is as follows.
Retrieve the Spreadsheet chart.
Retrieve the image blob from the chart.
Insert the image blob by copying the size and place of the Spreadsheet chart.
Remove the Spreadsheet chart.
Note:
This sample script copies the size and place. When you want to copy other values, please modify the script.
References:
asImage()
insertImage(blobSource, left, top, width, height)
remove()
Added:
About your additional question as follows.
I think this is a good workaround for charts (such as pie charts, column ... that can be converted as images). Nevertheless, I have some slides where I have some cells embedded. Running this code on this element is displaying an issue. Do you think this also feasible on embedded tables?
The chart is different from the table. So in this case, I think that your additional question is new question.
Your initial question is for removing the Spreadsheet link from the chart. My answer is for your this question. In this case, the table cannot be used. And, in the current stage, unfortunately, there are no methods for removing the link of Spreadsheet from the table. And also, when the Spreadsheet link is removed from the table, the object type is not changed from the table. By this, my workaround cannot be used. But Slides service and Slides API are growing now. So I think that such method might be added in the future update.
So, as the current method, how about reporting your new question to Google issue tracker? By this, the addition of such method might be considered.

How can you monitor for click events in Google Apps script for spreadsheets?

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.

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.