url argument error in my code for Google Sheets - google-apps-script

I borrowed and tweaked this code from another thread.
I have a button on the Google Sheets screen, which when clicked, creates a new UI window/screen with a clickable link, and the link is correctly referenced, but when you click that link it doesn't actually do anything.
The URLValue from AA1 would just be a concatenation of a Google Form address, with a code to pre fill the form with a number from my spreadsheet. The link all works as a cell content, and works fine from Chrome, but doesn't do anything in Safari.
function AddStock(){
var URLValue = SpreadsheetApp.getActiveSheet().getRange('AA1').getValue();
showAnchor('Add Stock',URLValue);
}
function showAnchor(name,url) {
var html = '<html><body>'+name+'</body></html>';
var ui = HtmlService.createHtmlOutput(html)
SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
}
Any idea where I'm going wrong?

Related

Launching Google Form from App Script

I am trying to launch an Google Form from a script on Google sheets. At the moment, i am doing it trough the form's unique ID and this is what I have:
function addtoDatabase() {
var formaddnew = FormApp.openById('uniqueformID');
}
I am sure i am using the correct form's ID. The script runs without returning any errors or exceptions but the form isn't launched. I am new to App script and I think I might be overlooking something.
Thank you for your help.
You (as me too) were confused with a strange name of function openById:
var formaddnew = FormApp.openById('uniqueformID');
This code does not "open" a form, it assigns a form to the variable formaddnew. You may check it if you add this line of code:
Logger.log(formaddnew);
run the code and press [Ctrl]+[Enter] to see the log.
How to open a form with a script
No. One has no such option because scripts have no access to a browser. A Form is actually opened in a browser, and google-apps-script cannot open new tabs in a browser.
Is there any way though to open a form from a pop-up or message box?
Please try the method described here:
Google Apps Script to open a URL
Here's a tested sample code based on this answer:
var C_URL = 'https://stackoverflow.com/questions/48947678/launching-google-form-from-app-script'; // change
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Menu')
.addItem('Show Window', 'testNew')
.addToUi();
}
function testNew(){
showAnchor('Open this link',C_URL);
}
function showAnchor(name,url) {
var html = '<html><body>'+name+'</body></html>';
var ui = HtmlService.createHtmlOutput(html)
SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
}
Notes:
the script creates a custom menu and opens the window with an URL.
User has to click the URL.

Opening a modal over another one

I use Google App Script with Google Sheets. When I click a button, the code enters in this following function :
function myButtonClicked(){
const template = HtmlService
.createTemplateFromFile('Index')
.evaluate();
const html = HtmlService
.createHtmlOutput(template)
.setWidth(500)
.setHeight(500)
.setTitle('MyTitle');
SpreadsheetApp.getUi().showModalDialog(html, 'MyTitle');
}
My form contains multiple inputs that have an onclick event. This event does something and displays a message in a specific case.
The message is displayed thanks to the following function :
SpreadsheetApp.getUi().alert('myMessage');
The problem is : when this last one appears, my modal containing my form is closed.
How can I display a modal over another one ?
Thanks.

Doesn't work - Cross-domain Redirect in google form in an iframe in google sheets app script

I have a function launchForm within a Google Sheet which calls another function createForm with a single question. I get the URL of the newly created form and pass it into a sandbox Iframe using UrlFetchApp, as shown below:
function launchForm() {
var form = createForm(); // separate function that works fine
formUrl = form.getPublishedUrl()
var response = UrlFetchApp.fetch(formUrl,{"followRedirects" : true}); // true if automatic redirecting works
var formHtml = response.getContentText();
var htmlApp = HtmlService
.createHtmlOutput(formHtml)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) // did this to prevent error but didn't help
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.show(htmlApp);
}
I have created a custom menu in sheets to run the launchForm function.
The trouble is, that the new form URL is at docs.google.com/a/, while the sheet is at docs.google.com/spreadsheets/d/...
This means that the iframe, instead of loading the form, opens up to a sign-in screen in when I (the creator of the form) tries to run it. When someone else with edit permissions runs the script, he sees the form as expected, but is then unable to make any inputs into the form.
I have read about CORS, X-frame-options, OAuth2, and nothing seems to provide the precise answer about what I must do. Any help will be much appreciated.

How to produce html text in UI popup using Google Apps Script?

I have the following Google Apps Script:
function popUp() {
const ui = SpreadsheetApp.getUi();
var message = "<HTML><BODY>"
+ "The meeting is at <b>10am</b>."
+ "</BODY></HTML>";
var popUP = ui.alert(message, ui.ButtonSet.OK);
}
When I apply this function to a button in my Google Sheets, the popup pops up as expected.
However, the text in the popup carries all the "messy" HTML coding.
How can I get the popup to show actual HTML formatted text?
In other words, how can I make the UI popup use the HTML-laden text I added for var message but in a way that actually formats the text to use the HTML I added?
IS there some kind of HTML wrapper function?
What I get vs what I want:
Here's a simple example closer to what you want. I don't think the alert takes html. It requires simple ascii text. Note: google.script.host.close() doesn't work when deployed as a web app.
function popUp() {
var s='The meeting is at <strong>10:30AM</strong>.<br /><input type="button" value="OK" onClick="google.script.host.close();" />';
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(s), 'PopUp');
}
See this in the 'Custom Dialog' documentation for Apps Script: https://developers.google.com/apps-script/guides/dialogs

I want to open a Link in my google document (no spreadsheet) directely by menu

I want to open a Link in my google document (no spreadsheet)
directely by menu or by function !!
How to arrive with my script ? it's simple but no one seems to understand
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('OAR')
.addItem('MAJ Date + OAR', 'test')
.addItem('MAJ Date Auto', 'testdate')
***.addItem('Accès Direct URL', 'href=/d/1udvLSpaZNgalg6NId JMJvkkw3i9oCaEKa- `A_CAC79CYkvsZKD545dyZN/edit?')***
//(By click on Menu 'Accès Direct URL' on screen it's open an other document (hyperlink) in a new window ... it's simple but no one seems to understand`
.addToUi();
}
This is simply not possible...
What you can do is let the menu open a popup in which you show a link that can be clicked and will redirect to a new browser tab/window.
such a code is pretty simple and goes as follows :
function showURL(){
var app = UiApp.createApplication().setHeight(50).setWidth(200);
app.setTitle("Show URL");
var link = app.createAnchor('open ', 'https://sites.google.com/site/appsscriptexperiments/');
app.add(link);
var ui = DocumentApp.getUi();
ui.showModalDialog(app, 'some title')
}