Passing and accessing URL variables in Google Sheet App - google-apps-script

I am passing a URL variable to a Google App Script that opens a modal dialog. How do I get the html page script to access that variable value?
// show html page for user selected row
// var i = the user selected row from the previous HTML page
function myFunction2(i) {
var html = HtmlService.createHtmlOutputFromFile('index2').setWidth(800).setHeight(600);
//I know the value is being correctly delivered here because
//it is being dumped out at the bottom of the page with this code
row = ['RowPicked', i];//This is not html
html.append(row);//This appends more html to the outout so it needs to be properly formatted html not google apps script code.
SpreadsheetApp.getUi().showModalDialog(html, 'Update Selected Row');
}

Related

Google-Sheets App Script to generate a custom email from inside Google Sheets

I have a google sheet which I use to send simple invoices. It is basic enough and it runs through an iPad (for fixed reasons) and this is the process which is kind of clunky :
-edit the template sheet which creates an invoice sheet (that's fixed obviously)
This is the awkward part :
-share/print/select one page (the invoice sheet)/print on a local printer
-share/print/select one page (the invoice sheet)/share (pdf) (edit mail)
I was wondering is it possible to have an apps script which :
-prints the first page of the sheets document, local printer
-opens the iPad mail program (not gmail)
-attaches the first page of the sheets document as a pdf
-changes the subject to a cell in the sheet (i.e. make the subject cell C1)
-changes the body to a cell in the sheet (i.e. make the body the contents of C2)
The question is strategic. If you have to control the final message version before sending, then use Gmail link to open "Compose window" with the corresponding parameters, as described here. Please note, you have no possibility to include any attachment by link in this case. You can (and should) do it manually.
Another way is to use templates (in drafts folder) which are more flexible for fill in data fields and for attachments too. The script can do your work completely but without the message preview. GmailDraft class has a useful getMessage() method for that and you can adjust details before sending. If you work with spreadsheets, you can adjust the script to show the message "preview" (similar to compose window) in sidebar or dialog window with a "Send" batton for confirmation.
Just in case, in addition to the strategic solution of Alexander Ermolin. As far as I know you can't print your documents in PDF via some printer. But you can make a PDF from spreadsheet pretty easy. Here is an example:
function myFunction() {
var ID = ''; // ID of your spreadsheet
var copy = DriveApp.getFileById(ID).makeCopy(); // make a copy of the spreadsheet
var ss = SpreadsheetApp.openById(copy.getId()) // open the copy
// convert all formulas into static values
var range = ss.getActiveSheet().getDataRange(); // get data range
var values = range.getDisplayValues(); // get all displayed values in the range
range.setValues(values); // paste the values back
while (ss.getSheets().length>1) ss.deleteSheet(ss.getSheets()[1]); // remove all sheets except the first one
DriveApp.createFile(ss.getBlob().setName('sheet.pdf')); // make a PDF file
copy.setTrashed(true); // delete the copy
}

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

Add custom Data to Google Sheet once Form is filled

I am using a Goofle form that receives applications from users, once the form is submitted I am using Google form submission trigger to notify. It has some questions like (facebook profile link)
But while sending a notification I don't want those links to be appeared just the same way user enters, instead something like Facebook, twitter (text as hyperlinked.) should appear
I already know about onFormsubmit() function, but not sure how to use, help me out please.
For example:
Cell A2 has user submitted facebook profile link using the form.
I want Column B2 to automatically generated as =hyperlink(A2, "Facebook")
The same thing should happen like A3 to B3 whenever user submits a form.
Ok after a bit of digging and testing, I believe I got a solution for you since you use the "Yet Another Mail Merge" (aka YAMM) add-on. Here it goes:
Firstly make sure your Form is setup properly and linked to a Google Sheet. After all questions have been created, add another column to your sheet, and call it 'Hyperlink' or whatever you please (just remember it for later). We will make use of the form submit trigger in the script editor along with some code.
Here's the code:
function onFormSubmit(e)
{
var r = e.range;
var v = e.namedValues;
var link = v['Link'];
// For testing purposes, this part was apart of my form,
// I'd assume you'd want to change it to something more
// usable in your case. Notice that I refer to the values
// by the name of the question they preside in.
var friendlyName = v['Friendly Name'];
var rngHyper = getCellRngByCol(r, 'Hyperlink');
// See below for the meaning of the boolean
addHyperlink(rngHyper, link, friendlyName, true);
}
// Will only return one cell no matter the range size.
// Perfect for onFormSubmit(e) use case.
function getCellRngByCol(rng, col)
{
var aRng = SpreadsheetApp.getActiveSheet().getDataRange();
var hRng = aRng.offset(0, 0, 1, aRng.getNumColumns()).getValues();
var colIndex = hRng[0].indexOf(col);
return SpreadsheetApp.getActiveSheet().getRange(rng.getRow(), colIndex + 1);
}
// Add some form of hyperlink reference to one particular
// cell, passed as a range object
function addHyperlink(rng, link, name, useFormula)
{
if (useFormula)
{
// If useFormula is TRUE, use Google Sheet HYPERLINK formula,
// only if you are sure all URL's are formated properly,
// and include HTTPS/HTTP/WWW. Also looks more pleasing in Google Sheet.
var formula = '=HYPERLINK("<<URL>>", "<<NAME>>")';
formula = formula.replace('<<URL>>', link).replace('<<NAME>>', name);
rng.setFormula(formula);
}
else
{
// Else use HTML <a> tag with hyperlink referencing, which should transform
// any URL passed as a clickable hyperlink within email. Not very visually
// appealing in Google Sheet.
var value = '<<NAME>>';
value = value.replace('<<URL>>', link).replace('<<NAME>>', name);
rng.setValue(value);
}
}
Then set the trigger, which will probably ask for authorization after saving:
Next, save the script and then put in a test submission through your form to see that the link is created properly, or as desired. Afterwards clear the rows of the spreadsheet (not the header) and remove all responses of the Form itself (not necessary, but keeps things organized for testing purposes).
Now, install the YAMM add-on. It should then add a new column at the end of your sheet called 'Merge satus'. Before setting up the email notification on submit, we need to create your email template. Open up GMAIL, create an email with the desired fields and layout and save it as a draft. Here's what I did as an example:
I'm sure you're familiar with how this add-on, works so I shouldn't need to explain too much here.
After the draft has been created and saved, go back to the Google Sheet attached to the Form. Go to Add-ons > YAMM > Configure form submission notifications. I chose the 'Notify one or more addresses of all responses' option which are tied to the 'To:' emails preset in the draft. Select your draft from the drop down, fill in sender name if needed, AND (very important!!) check the 'Wait until a specific column is filled before sending the email' check box. Make sure to select the Hyperlink column (or whatever you chose to name it earlier). Here's my setup for reference:
Save it, test it, and ta-da. This simple formatting for hyperlinks has been resolved! :D

Update Anchor from function

In my process, I have a page that the user can select various items from a Listbox. These values are a Cell in a spreadsheet. A different column on that spreadsheet contains the URL of the spreadsheet. So after the user selects the value from listbox, the cell containing the URL is stored to a variable using a createServerClickHandler. What I can do already, is pass that URL back to a Textbox on the main page displaying the URL. What I'd like to do is have an Anchor appear on the main page so that, the user can click on it to open the spreadsheet that is the currently selected on in the Listbox.
var urlText = 'Click here to see selected spreadsheet';
var urlLink = bldrapp.createAnchor(urlText, **urlURL**).setId('spreadsheetURL').setVisible(true);
From my function(e)
app.getElementById('infoURL').setText(**docToProcessURL**).setVisible(true);
The 'infoURL' is my Textbox on the main page. Is it possible to pass that value up to the 'urlURL' in the anchor?
Thank you,
Yes, you can do it like this:
app.getElementById('spreadsheetURL').setHref(docToProcessURL);

Can I add a hyperlink inside a message box of a Google Apps spreadsheet [duplicate]

This question already has answers here:
Google Apps Script to open a URL
(6 answers)
Closed 2 years ago.
Is there a way to add a hyperlink inside a message box of a Google Apps spreadsheet?
I have this code that displays a msgbox.
// The code below will display a message box
Browser.msgBox("Go to this site for help");
}
Is there a way to insert a hyperlink in that message box as well? Something like:
// The code below will display a message box
Browser.msgBox("Go to this site for help" & Help);
}
Google's UI service is deprecated as of Dec. 11, 2014. See here.
You should now use the HTML Service. The code to display a message with a link is below.
var htmlOutput = HtmlService
.createHtmlOutput('Go to this site for help!')
.setWidth(250) //optional
.setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Help Dialog Title');
It appears Google Sheets won't run scripts for anyone who opens a public spreadsheet (obviously for security). If you want to see a live version of the dialog, just copy the above code into function onOpen() {} in the script editor, save & refresh the spreadsheet. Otherwise, it looks like the image below.
If you have more HTML than a simple link, you can also create a dialog from an HTML file. In the script editor, select File > New > Html file and name it "index" (or whatever you want and change file name in code).
var html = HtmlService.createHtmlOutputFromFile('index');
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Dialog title');
This is an example of a popup showing a link to an url
function showurl() {
var app = UiApp.createApplication().setHeight('60').setWidth('150');
app.setTitle("Anchor in a popup ;-)");
var panel = app.createPopupPanel()
var link = app.createAnchor('This is your link', 'https://sites.google.com/site/appsscriptexperiments/home');
panel.add(link);
app.add(panel);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
Sorry. Message boxes do not accept hyperlinks or tags. Plain text only.