How do I open a web browser using google apps script? [duplicate] - google-apps-script

This question already has answers here:
Google Apps Script to open a URL
(6 answers)
Closed 2 years ago.
I've created a spreadsheet in google drive, and I've put together a tutorial presentation using google presentations to demonstrate to the users how to use the spreadsheet and why it's better than the way we were doing it before.
What I would like is a message box to appear when the spreadsheet is opened asking if that person has watched the tutorial presentation yet. If the user clicks No I'd like to either open a new page in the browser to show them the presentation I've published, or show the presentation in a custom UI.
I've searched for hours and can't figure it out. Is this possible? Thank you for your help!
I'm new to the forum so please let me know if I posted this wrong. Thanks!

Something like this does it pretty well.
I used a fake button to keep the "look and feel" consistent with the other button , I simply added the link invisible on top of it ;-)
function alertLink() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Message').setHeight('100').setWidth('400');
var panel = app.createVerticalPanel().add(app.createHTML('Did you see my beautiful <b>Tutorial</b> ?'));
var grid = app.createGrid(1,2).setWidth('400');
var closeHandler = app.createServerHandler('close');
var b1 = app.createButton("NO and I'd like to").setTitle('go to the tutorial in a new tab');
var b2 = app.createButton("YES and I don't want to see it again",closeHandler).setTitle('close this window');
var link = app.createAnchor('XXXXXXXXXX','http://www.google.com').setStyleAttributes({'zIndex':'1' , 'position':'fixed' , 'top':'25' , 'left':'20', 'color':'transparent' }).setTitle('go to the tutorial in a new tab');
var G1 = app.createVerticalPanel().add(b1).add(link);
grid.setWidget(0,0,G1).setWidget(0,1,b2);
app.add(panel).add(grid)
doc.show(app)
}
function close(){
return UiApp.getActiveApplication().close();
}
Just use an onOpen trigger if you want it to execute automatically on spreadsheet open. (or rename the main function as onOpen() )
Here is how it looks like :

For an onOpen trigger, it's certainly a better UX to prompt the user with a dialog box offering the option to open a URL, as opposed to opening the URl automatically. See serge's answer here for how to do that (which is more up-to-date than the deprecated code in his answer on this thread).
But for other triggers (such as a menu item click) check out my answer here to open a URL automatically.

Related

Create Custom Buttons in Google Apps Script for Google Docs

I am trying to find a way to create my own custom text for a ButtonSet in Google Apps Script. I do not want to use one of the default options of YES, NO, OK, etc. I would like the buttons to read: 'Today' and 'Tomorrow'. This is what I have tried so far, but I am having no luck...any thoughts on how I could accomplish this? Thank you!
// Create custom button text for Today and Tomorrow
var TodayButton = CardService.newTextButton()
.setText("Today");
var TomorrowButton = CardService.newTextButton()
.setText("Tomorrow");
var DayButtonSet = CardService.newButtonSet()
.addButton(TodayButton)
.addButton(TomorrowButton);
var responseWeatherImpactsDate = ui.alert('Do you want the first day of the Weather Impacts table to be today or tomorrow?', DayButtonSet);

Display Data from Google Sheet into Google Forms

I am creating a Quiz.
What I did first is to create data in google sheet and here it is
Link of Google Sheet
as you can see there I have 2 sheet and that is Questions and Answers
My Question is how can i display the Question and Answers in there respective position in Google Forms i am linking them by Question ID
TYSM
I think the comments above will help you. This is also code (with help from here) which copies a multiple-choice question from one quiz to another.
If you combine this code with the stuff referred to in the comments about reading stuff from a sheet then you should be ok.
I see that you mention a problem copying images. I'm stuck at that point too.
function copyMultipleChoiceItem(item1, item2) {
// copies MC question item1 to item2 - tested PDW 17/05/20
// copy of feedback now working - tested PDW 17/05/30
//
var item1MC = item1.asMultipleChoiceItem();
// basic question items
item2.setTitle(item1.getTitle());
item2.setHelpText(item1.getHelpText());
item2.setPoints(item1MC.getPoints());
item2.setRequired(item1MC.isRequired());
// the choices
var choices = item1MC.getChoices();
for (var i = 0; i < choices.length; i++) {
item2.createChoice(choices[i].getValue(),choices[i].isCorrectAnswer());
}
item2.setChoices(choices);
// the feedback
var feedback1 = item1MC.getFeedbackForCorrect();
item2.setFeedbackForCorrect(feedback1);
var feedback1 = item1MC.getFeedbackForIncorrect();
item2.setFeedbackForIncorrect(feedback1);
}
I'm not fully cleared about your question, but If you want an easy way to do a relationship between Google Sheets (columns) and Google Forms Drop-down lists and other Forms objects, you can try this add-on: formRange. Build your sheets then go to your Form and Addons. Chose formRange and install it. It's pretty easy to use and you have a little tutorial inside it.
formRanger: Google Form addon

Google apps script editor's content assist is great, but buggy. Tips n' Tricks? Is this right forum for this?

Is this right place for google script editor questions? Hope so.
Content assist is great, but stops working at times for me. Sometimes fix is to cut/paste all my code in/out of a desktop text editor, and back into google script editor. Perhaps this cleans out hidden chars, tags, etc., or perhaps it resets content assist. Dunno. But, works somewhat. Any thoughts? Tips? Trick?
Too, here's great crash course on google script editor from the developers. Well worth a the watch: Crash Course Apps Scrip Editor
If not appropriate place for editor questions, please point me to it. Thanks.
Also, does GAS stand for google apps script? google apps services? A library? Wha? Yes, I'm kinda newbie. Is there an apps script related wiki?
One trick to reset the content assist is to go back up to the class and retype a period right after it. So, if you're working with a line of code that involves Sheets and it has lost auto-complete for what ever reason, typing the period right after the firstSpreadsheetApp in the function has worked for me.
SO is the wiki.
Please don't use a "GAS" for abbreviation.
I tried re-typing the period after SpreadsheetApp and it didn't seem work for me. I was chaining variables together like this:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
open_log = spreadsheet.getSheetByName('OPEN_LOG'),
closed_log = spreadsheet.getSheetByName('CLOSED_LOG'),
invoiced_log = spreadsheet.getSheetByName('INVOICED_LOG');
Once I changed the code to:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var open_log = spreadsheet.getSheetByName('OPEN_LOG');
var closed_log = spreadsheet.getSheetByName('CLOSED_LOG');
var invoiced_log = spreadsheet.getSheetByName('INVOICED_LOG');
the content assist started working again. I'm not 100% positive this is the fix, just seemed to work for me this time. Give it a try...beats pulling your hair out.
This seems to work for me when I lose CONTENT ASSIST [object method prompting] ... for the moment ... the getfid function may somehow be used by the editor when following the actual 2 steps in the function beneath it.
function getfid(fname) {
files = DriveApp.getFilesByName(fname);
file = files.next();
fid = file.getId();
return fid;
}
// the following 2 steps seem to turn CONTENT ASSIST back on ???
// 1. type the period after the SpreadsheetApp [ss must exist?]
// 2. then test to see if TA is back on by typing the period after ss_ad
function resetCONTENTASSIST() {
ss_ad = SpreadsheetApp.openById(getfid('adminDATA'));
ss_ad.
}

google apps script DateItem get dropdown box

So using Google Apps Scripts with a Form, I'm able to get all the items and iterate through them using the following:
var form = FormApp.getActiveForm();
var items = form.getItems();
var item;
for(var i = 0; i < items.length; i++)
{
item = items[i];
if(item.getType() == FormApp.ItemType.DATE)
{
item = item.asDateItem();
item.dropdown.month; // I need a method like this
}
Logger.log("ItemTitle: %s ItemType: %s",items[i].getTitle(), items[i].getType()) ;
}
I can even get the DateItem that I want.
My issue is that I cannot get the dropdown boxes from the DateItem. Does anyone know how to get the dropdown boxes from the DateItem? (Like: item.dropdown.month or item.dropdown.day, etc).
FormApp describes the questions you're asking, not the answers received from people who filled the form out. The type of question is "when were you born?" - nobody answered it yet - there's no date available.
When the form submits, its results go into a spreadsheet. Write a script that parses the spreadsheet's content.
For this kind of dynamic form behavior (a form that responds live as users fill it out), your best bet is to build and HTML user interface using the HTMLService: https://developers.google.com/apps-script/guides/html/
This is significantly more complicated than working with the FormApp Class, but is much more powerful, and eventually allows you to deploy your code as a web app, or publish as an add-on for Sheets, Docs, or Forms.

Is there a way to remove a script from a doc (using the new doc embedded script)

I developed a script extension that uses a Google doc as template AND as script holder.
It gives me a very nice environment to implement a mail merge application (see below).
At some point I use the DocsList class makeCopy(new Name) to generate all the docs that will be modified and sent. It goes simply like that :
var docId=docById.makeCopy('doc_'+Utilities.formatString("%03d",d)).getId();
Everything works quite nicely but (of course) each copy of the template doc contains a copy of the script which is obviously not necessary ! It is also a bit annoying since each time I open a copy to check if data are right I get the sidebar menu that opens automatically which is a time consuming process ...
My question is (are) :
is there any way to remove the embedded script from the copy ? (that would be simple)
or should I copy all the doc elements from the template to an empty document ? (which is also a possible way to go but I didn't try and I don't know what will be in this doc in real life use...
Shall I get a perfect clone in any case ?)
I've read the doc and didn't find any relevant clue but who knows ? maybe I missed something obvious ;-)
below is a reduced screen capture to show the context of this question :
Following Henrique's suggestion I used a workaround that prevents the UI to load on newly created documents... (thanks Henrique, that was smart ;-)
The function that is called by onOpen now goes like that :
function showFields() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var find = body.findText('#'); // the new docs have no field markers anymore.
if(find != null){ // show the UI only if markers are present in the document.
var html = HtmlService.createHtmlOutputFromFile('index')
.setTitle("Outils de l'option Publipostage").setWidth(370);
ui.showSidebar(html);
}
}