GmailApp to .showModalDialog script in sheets? - google-apps-script

I want to show the GmailApp 'create email' UI on a .showModalDialog method in Google Sheets.
So far, I have been able to create a mail merge macro that works perfectly fine with script editor & various HTML files. However, my next task is to allow the user to draft an email in Google Sheets within a .showModalDialog
Here is a screenshot from the popular MailMeteor, as to show you what I would like to emulate.

Show Modal Dialog
function showModalDialog() {
var html="";
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), 'Modal Dialog');
}
UI

Related

google docs linked charts refresh with api or apps script

I have a google doc that I show inside an iframe in my website for public editing, it contains linked charts from a public spreadsheet,
I change the chart data with spreadsheet api, but the chart in the doc does not refresh automatically, and I can't clic update or update all in the doc because the "update button" is disabled,
SO I wanted to use google docs api or apps script to refresh charts,
I did not find how to do it with google docs api, and apps script does not trigger inside the iframe,
Can you please give me any idea how to do this,
Thank you.
Unfortunately there is no way to refresh charts programatically in a Google Document. You should definitely submit a feature request for SheetsChart support as there is in Google Slide App Script Service.
Workaround with Slides
Since the update feature is currently available for Slides you could achieve to style a Slide as a document and use it in your iFrame. Following this update logic you will be able to trigger a SheetsChart refresh every minute for example:
function myFunction() {
ScriptApp.newTrigger('updateCharts').timeBased().everyMinutes(1).create();
}
function updateCharts() {
let slides = SlidesApp.getActivePresentation().getSlides();
slides.forEach(slide => {
let charts = slide.getSheetsCharts();
charts.forEach(chart=> chart.refresh());
});
}
Workaround with Documents
However, since your goal is to provide your website viewers with the last version of the iFrame this can be done automatically on Document save:
File > Publish on the Web > check Automatically republish when changes are made
You will have to update manually the chart in your Document though. But this change will be reflected in your webpage automatically.

Button that performs a function (on slides)

I saw that in the spreadsheet you can create a drawing and link to a function in the script.
I need to: Create a button or link where clicking it will perform a function in the script.
It will be a simple email sending function.
Example: When the user clicks the button or link in the presentation, they receive an email from them.
There isn't a way to assign a script to a button in Google Slides in the same way as there is in Google Sheets.
The only alternative would be to use either a simple or installable trigger depending on your needs.
Google Slides only has compatibility with the onOpen() and onInstall() simple triggers which you can see here, though you would be able to send the mail with the built-in Apps Script library as such:
function sendEmail() {
MailApp.sendEmail("username#domain.com", "subject", "body");
}

Displaying a Google form on sheets

I am creating a form for users to input information on a Google spreadsheet. They will access the spreadsheet and then click on an image that is linked to a script. When the image is clicked, I want a form to appear. Then I want the input from the form to be accessible in the script.
I am able to create a form but the form does not appear on the sheet. Here is the code thus far for the GS script
function startForm() {
var form = FormApp.create('myForm');
var item = form.addCheckboxItem();
item.setTitle('What would you like to do?');
item.setChoices([
item.createChoice('Budget Inquiry'),
item.createChoice('Add Purchase')
]);
var choices = item.getChoices();
// then I can respond to the user's choice
}
I would like this simple form to just appear on the google sheet. Any input would be appreciated.
Instead of creating you own form with script, just go to the insert menu of your spreadsheet and select form. Enter you question and your two choices. Close the form. You will see a form response sheet created in your spreadsheet. Also, a menu item Form will appear on your menu. Then go to the script editor and from the menu select Resources. Select Current Project Triggers and set a new trigger for onFormSubmit. You can then enter a function onFormSubmit to do whatever you want done when the form is submitted getting data from the form response sheet. There is plenty of documentation you can Google.
The way in Google Sheets to display external content other than images and Google Drawings is by creating a dialog or sidebar with the related content embeded.
There is a similar question that includes an answer that shows how to do this:
Single Google Form for multiple Sheets

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();
};

Attach a google form to a google spreadsheet

I'd like to create a form by google script.
This is pretty easy but I'd like to attached this form to a spreadsheet (no only the response), like I would have created it by the UI.
Is that possible ? Given the Formclass or FormApp is doesn't seem so. Is there a way around ?
EDIT: My goal was to create the form with the script and having the same result as if the user had had the form created from the UI interface (the main difference being having the form menu in the SS UI). It's apparently not possible.
You're right - it's pretty easy once you have the trick. Use FormApp to get the published URL of the form, UrlFetch to grab its html, then the HtmlService to present the form in the spreadsheet's UI.
See Single Google Form for multiple Sheets.