Keep Macro initiating drawing always on the screen - google-apps-script

In my Google Sheets I have two drawings, each of which executes a macro. As I scroll through the sheet I would like the "drawing" to follow me so they always appear on the display so I can invoke them immediately. Is there a way to make these "macro initiating drawings" float so they are always on the screen?

At the moment, this is not possible. However, I would like to propose you two alternatives using Google Apps Script.
Creating a menu
You can create a menu in Google Docs, Sheets, Slides or Forms using Google Apps Script.
In order to do so, you must create an onOpen() function containing code for the creation of it. Each of the buttons can execute different scripts that you may choose.
If you would like to create a menu with two options (one for the first drawing, and one for the second one) you could use a code similar to the following:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom menu')
.addItem('First item', 'yourFirstFunction')
.addItem('Second item', 'yourSecondFunction')
.addToUi();
}
Where yourFirstFunction and yourSecondFunction should be functions declared in your script. It would look like the following:
Creating a sidebar
Sidebars can display HTML content (including images) so they may be more akin to what you are trying to accomplish. In order to create them you will also need to use the onOpen() function to trigger its display:
function onOpen() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
Along with the declaration of the two functions, yourFirstFunction() and yourSecondFunction().
You will also need to create your HTML file. You can create via File>New>HTML File, and for this example we will call it "Page". It will hold the following contents:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<img src="https://drive.google.com/uc?export=view&id=1eUiSE5jLHWQiOTQISbdsTxsOLgoOvxLM" width="250" onclick="google.script.run.yourFirstFunction()"/>
<img src="https://drive.google.com/uc?export=view&id=1pZCYQH1ZkxxL3Uvw8q9aHvp7cc7pWMAS" width="250" onclick="google.script.run.yourSecondFunction()"/>
</body>
</html>
As you can see, it holds two images, each of one calling their respective Google Apps Script functions. This would look like the following (of course you can use the images you please and customise the sidebar as much as you want using HTML5 and CSS3):
References
Custom Menus in G Suite
Custom sidebars

Related

Adding an html element directly into google sheets cell using google apps script

Is it possible to add an html element directly into google sheets cells using google apps script? I have a tasks list in my minitask.html that I would like to add into my google sheet. I have a large merged cell where I would like to add the element (B39:I71). I haven't been able to find a way to add it directly into my merged cell.
The best solution I have found that gets the task list somewhat on to my sheet is by adding a sidebar of the element.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Tasks')
.addItem('Mini Tasks', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('minitask')
.setTitle('Mini Tasks');
SpreadsheetApp.getUi()
.showSidebar(html);
}

How to replace generic icon

I am creating an add-on for Google Forms. The script extracts data from the form and sends the results to the Google spreadsheet. What I would like to do, if possible, is replace the generic add-on icon (refer to the screenshot below. The icon is highlighted) with an icon that I have. How would I achieve it?
The following is where I am creating the add-on menu:
function onOpen(e) {
FormApp.getUi()
.createAddonMenu()
//.createMenu("MOATT Add-On")
.addItem('Show Modal Extract Form Q&A', 'showModal')
.addItem('Show Sidebar Modal Extract Form Q&A', 'showSideBar')
.addItem('Show Modeless Extract Form Q&A', 'showModeless')
.addItem('About MOATT Add-On', 'showAbout')
.addToUi();
}
Google Apps Script have methods to add custom menus but not to change the built-in user interface.
Resources
https://developers.google.com/apps-script/reference/forms/form-app#getui
https://developers.google.com/apps-script/reference/base/ui.html

google-apps-script restric menu use/creation to users

I have a sheet with a google apps script that does various things. One of those things is building a menu on open.
I would like this menu to be built only when a certain specific user is acessing the sheet. How can I achieve this?
You cannot have restrictions inside onOpen since the user's email address is not available in any context that allows a script to run without that user's authorization. You can however include checks in the function that runs on clicking menu items.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Menu')
.addItem('First item', 'doSomething')
.addToUi();
}
function doSomething() {
if (Session.getActiveUser().getEmail() !== "abc#example.com") return;
// else do something here
}

Google Apps - Script - Side bar OnOpen/OnInstall function

I'm recently having issues with Google Apps Scripting, I have a script which installed domain wide, which is enables users to make some tasks easier with the side-bar menu however from last week side-bar menu is not popping up anymore. Nothing has been changed on the code.
I did some workaround and tried all possible ways - deauthorization \ authorization, deploying the same code with different name, nothing worked except if the user removes it and adds it to the spreadsheet which they work - it's popping up and working only for that sheet and only for one time.
Can you please advise, how to fix this issue?
Here is my OnInstall and OnOpen functions:
function onInstall(e)
{
onOpen(e)
}
function onOpen(e) {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
Thanks in advance.
Edit : Triggers making the side-bar work on simple scripts on spesific spreadsheets, but this method is not working on domain-wide installed add-ons.
It should be possible to add a trigger to run the custom function which shows the SideBar.
I have a function like the following in my code that runs on my domain:
function createOnOpenTrigger()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
// Trigger on open
ScriptApp.newTrigger(FunctionToRunOnOpen)
.forSpreadsheet(sheet).onOpen().create();
}
function FunctionToRunOnOpen()
{
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
You could then put the createOnOpenTrigger function as an option in your custom menu - to be run only once everytime a copy is made of the entire spreadsheet.

Display Form in Sidebar

I am not sure if what I am trying to do is even possible.
I am trying to put a google form into the sidebar of a google doc.
Right now I have an apps script document plugin that opens the sidebar fine, and I am able to open the form using var form = FormApp.openById('form_id');
But I don't know how to get a html object or blob from a form element that I could use to embed the form in the sidebar.
I also can't use iframes as those are disallowed.
While you can display a google form in a sidebar in a spreadsheet/document (see sample code below), there is one significant gotcha with it: the form does not work, i.e. you can't submit the form. Not sure exactly why, but my guess it is due to the sandbox restrictions and/or caja sanitization.
I suggest you code your own form using HTML and HTML Service (or UI Service) and show that in a sidebar. If you need you form to save the responses to a spreadsheet, you can also do that rather easily. See this for more on info and code samples of using forms in HTML Service and this for using forms in UI Service. Your server-side script can open a spreadsheet and write form values to it, if that's what you need to do.
Sample code for an Add-on to show Google Form in a Sidebar in Google Sheet:
NOTE: The form does not actually work - it does not submit when shown in a sidebar!
function showFormInSidebar() {
var form = FormApp.openById('YOUR-FORM-ID-HERE');
var formContent = UrlFetchApp.fetch(form.getPublishedUrl()).getContentText();
var ui = HtmlService.createHtmlOutput(formContent).setTitle("Google Form in Sidebar Example");
SpreadsheetApp.getUi().showSidebar(ui);
};
function onOpen(e) {
// Add this add-on to Add-ons menu
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Show form', 'showFormInSidebar')
.addToUi();
};