Display Data from Google Sheet into Google Forms - google-apps-script

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

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

How to get an "image in cell" from a spreadsheet and copy it into a document as INLINE_IMAGE?

I've set-up a spreadsheet as a DB to store catalog-like information. An important element of this information is an image the end user inserts in a user-front sheet using the Google Spreadsheet's menu option "Insert --> Image --> Image in cell". This image is then copied into the storage sheet using "SpreadsheetApp.CopyPasteType.PASTE_VALUES" in a script.
Now I'm crafting a document as an output of the information stored in the DB. The issue is that I can't parse the image from the spreadsheet to the document. I'm not even being able to get it from the spreadsheet.
I've surfed the web for a solution but got nothing. The closest I found is a 2 year-old similar question without a direct response: how to copy IMAGE from a google spreadsheet to a google document in script?
My logic says that if I pasted the image-in-cell as a value, I should be able to get it back with .getValue() but obviously is not the case. I've tried to get it as a blob with no luck. I'm not too familiar with blobs so probably I did it wrong.
Any idea will be much appreciated :)
// This is the code I used to put the image in the cell (THIS WORKS, JUST FOR CONTEXT)
var fotosToCopy = cotizador.getRangeByName('CotizacionFotos');
fotosToCopy.copyTo(destino.getRange(fila,2),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, true);
// This is the code I'm trying to get the image from the cell (NOT WORKING)
var fotosToCopy = origen.getRange(personajeRow,2).getValue(); //I've tried .getFormula() with no results; origen is the search range, personajeRow is the row to do the search and both work.
// This is what I'm using to put the image in the document
var doc = DocumentApp.openById(cotizacionId); // cotizacionId is working
var body = doc.getBody();
var foto = body.getImages(); // I'll replace the only existing image
var parent = foto[0].getParent();
parent.insertInlineImage(parent.getChildIndex(foto)+1, fotosToCopy[0]); //fotosToCopy[0] is the issue, it returns "undefined"
foto[0].removeFromParent();
Ideally, the existing in-line-image in the document should be replaced by the image-in-cell from the spreadsheet. The error I get is: "Execution failed: Cannot convert Array to Element".
Any idea?
so I haven't coded this very cleanly but it should give you somehtign close to what you want, but first an explanation:
Google Sheets doesn't have the capacity to return the blob of an image, only a reference to the image, but Google Drive does.
My solution below takes the blob of the FIRST image in the Google Drive folder and adds it to the Document based on the DocID I provide to the function:
function retriveFolder(folderName,imageName,docID){
var imageFolder = DriveApp.getFoldersByName(folderName).next();
var firstImageinFolder = imageFolder.getFilesByName(imageName).next();
var imageBlob = firstImageinFolder.getBlob();
DocumentApp.openById(docID).getBody().insertImage(1, imageBlob);
}
I hope this helps, best I could come up with when I had the same problem. Not ideal but should be a suitable workaround.

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.

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

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.

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