I am working on a dashboard in Report Studio with list prompts that will load different reports. I have a single list prompt and an HTML button that will launch the report selected from the list. I am trying to add additional list prompts for different report categories. The issue is that all of the HTML buttons will only look at one of the list prompts. The excerpt from the script is as follows. I change the .getControlByName() to reference my different list names, but that doesn't help.
<script>
function open_win()
var oCR = cognos.Report.getReport();
var myPrompt = oCR.prompt.getControlByName("classlist");
var v = myPrompt.getValues();
var selectedValue =v[0]['use'];
window.open(selectedValue);
return true;
I am not familiar with java script, so I am guessing at what these commands do.
Each prompt needs its own Name property populated. Assuming you've done this is what the code would look like:
function open_win(section) {
var oCR = cognos.Report.getReport();
var value = oCR.prompt.getControlByName(section).getValues()[0].use;
window.open(value);
}
For this to work, you'd have one HTML button for each section.
In each HTML button's onClick definition, call the universal open_win() function, passing in the name of the associated section which needs to match the associated prompt: e.g. open_win('classlist').
Related
I am a teacher trying to create a registration system with Google Forms, Sheets, and App Script. I have tried using THIS THREAD to figure out the App Script I need to automatically update choices based on form submissions.
The Google Form will include class choices for Block 1, Block 2, Block 3, etc. Students will choose a class for each block and then submit the form. The Apps Script will look at the response spreadsheet, see that Class 1 for Block 1 is full, then removes it from the list of possible choices. The next student goes to register, and their only option is Class 2.
Based on the linked thread (see above), I tried this code. It seemed to work for one question... or so I thought. I linked my Spreadsheet here and include the Code Snippet.
Link to Spreadsheet -- Available Classes in "formMaker" Sheet. (if there's a better way to do this, please let me know)
Code:
function updateForm(){
var form = FormApp.openById('1Mrh79XlB_EX6jkK_dOX9sVIwAxTImqS8X2dIfEE0Juw'); // Base form
// need to read what dates are available and which are taken
var doc = SpreadsheetApp.getActiveSpreadsheet();
var dates = doc.getRange("FormMaker!A2:A").getValues(); //available options
var taken_dates = doc.getRange("Form Responses 6!C2:C").getValues();
// joining the taken dates into one string instead of an array to compare easier
var taken_dates_string = taken_dates.join("|");
var choice = [];
// loop through our available dates
for (d in dates){
}
var formItems = form.getItems(FormApp.ItemType.LIST); // our form list items
// assumption that first select list is the one you want to change
// and we just rewrite all the options to ones that are free
formItems[0].asListItem().setChoiceValues(choice);
}
Here is what I want to achieve:
I want to delete 'n' number of rows from my google spreadsheet document. This 'n' can vary depending on number of wrong entries inserted in the document (I know this number before running the function). And I want to give myself a flexibility to choose this number (just like console input in C, C++ or any other languages).
Some researching shows solution via SpreadsheetApp.getUi() mode. But it is giving me error: Exception: Cannot call SpreadsheetApp.getUi() from this context.
I don't want to open my spreadsheet as it is huge in size & takes time to load. Purpose of deleting rows pragmatically is that I don't have to open it, else its all 'moo' point.
Another solution could be to just create an variable and change is manually before running script. But it could create bad data if I forget to change that variable someday (I want to make it idiot-proof).
Is there any way to get user input for standalone google app script? and without opening that particular google sheet?
You can always put the script into a blank sheet and treat it as a placeholder for your functions and have the ui prompt pop there. This way, you don't need to open your large sheet. You can always access other sheets when in another via Apps Script. This would be easier and you just need to transfer your script here.
Code:
function showPrompt() {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
'Rows to delete?',
'Input:',
ui.ButtonSet.OK_CANCEL);
var button = result.getSelectedButton();
var numRows = result.getResponseText();
if (button == ui.Button.OK) {
// call function and pass the value
deleteSheetRows(numRows);
}
}
function deleteSheetRows(numRows) {
// url of the sheet with data
var url = "https://docs.google.com/spreadsheets/d/***************/";
var sheet = SpreadsheetApp.openByUrl(url);
// do what you need to do here for that sheet using "numRows" value
Logger.log("deleting "+numRows+" rows");
}
Output:
You can create a function in web app just write doGet() or doPost() function and call it with your input.
refer https://developers.google.com/apps-script/guides/web
Take input number of rows which is n in your case, and add your code to delete rows from SpreadSheet.
you can pass input for get by using query parameter like:
?n=4
and you can use n in doGet() method.
I have a google form where I have a multiple choice question "Territory field". This field is having two valid answer
United States and
Others (When you choose other option you can write down any Country name in text area)
Now I am trying to submit this form using google app script. While submitting from app script if I am sending answer to this multiple choice question as "United States" its workiing. I want to send Others as answer to this question while submitting the form. Can anyone help me out. I am including picture of field and script below for reference .
Picture of form field with multiple choice
I am trying to submit this form using google script (part of script mentioned below).
var formID = "xxxxxxxxxxxxxxxxxxxxxx";
var Formobj= FormApp.openById(formID);
var formResponse = Formobj.createResponse();
var items = Formobj.getItems();
var territory = items[1]
var territoryvalue = "United States"
if (territoryvalue.indexOf("United States") !== -1)
{
var territoryfield = territory.asMultipleChoiceItem()
var territoryresponse = territoryfield .createResponse([["United States"]])
formResponse.withItemResponse(territoryresponse);
}
Field value after form is submitted with United states as answer
Need help in submitting this form with "Others" as answer
The documentation for MultipleChoiceItem.createResponse() reads:
Creates a new ItemResponse for this multiple-choice item. Throws an exception if the response argument does not match a valid choice for this item, unless showOtherOption(enabled) is set to true.
So to submit an "other" response, just pass it the value.
var territoryresponse = territoryfield.createResponse([["Other Country"]])
If instead you want to add a new value as a selectable choice in the form, then you can create a new choice and then set the item choices.
var territory = items[0].asMultipleChoiceItem();
var newCountryChoice = territory.createChoice("Another Country");
var choices = territory.getChoices(); // Get the existing choices
choices.push(newCountryChoice); // Append the new choice to the list of existing
territory.setChoices(choices); // Set the updated choices
I saw you filed a bug in Issue Tracker, and based on your explanation there I could finally understand this issue.
This seems to be a bug:
In Forms, if a Multiple choice item gets submitted with an Other choice via Apps Script, this other choice is not populated when trying to edit the response. If the response is submitted via UI, the Other option is populated.
It's important to note that the response itself is getting submitted successfully, and can be seen in the Responses tab of the Form; it's just not populated when trying to edit the response.
Steps to reproduce:
Create a new Form.
Create a Multiple choice item for the Form, if there is not one already.
Click add "Other" in order to have an Other option.
Click Settings (gear icon) and check the option Respondents can: Edit after submit.
Open the script editor bound to the Form, and copy and run the following code:
function submitAndGetEditResponse() {
var form = FormApp.getActiveForm();
var item = form.getItems()[0].asMultipleChoiceItem();
var otherOption = "Option 2"; // Not one of the named options
var itemResponse = item.createResponse([[otherOption]]);
var formResponse = form.createResponse().withItemResponse(itemResponse);
var editUrl = formResponse.submit().getEditResponseUrl();
console.log(editUrl);
return editUrl;
}
Access the URL (editUrl) with the browser.
Option 2 should be populated when trying to edit the response, but it's not.
Issue Tracker:
The issue you filed in Issue Tracker just got forwarded internally by Google:
Google Form Submission Through Google App Script - Multiple Choice Question's Other Option Not Populating
Anyone affected by this, please consider clicking the star on the top-left in order to keep track of it and to help prioritizing it.
I tried replicating your code and got the solution for what you wanted.
If you want your "Other" answer to be ticked in the individual responses. Your response string should look like this: "other_option (Country)"
For example, if you want Mexico to appear in the "Other:" option, you should write "__other_option Mexico" as the value for your territoryvalue variable. It should look like this in that part of your code:
var territoryvalue = "__other_option__ Mexico"
I'm a high school teacher in L.A. trying to create a course registration system using Apps Script. I need the Google Form I'm using for this registration to:
Question 1) Update the choices available in subsequent multiple choice questions on new pages based on a student's current response choices.
Question 2) Eliminate choices from the form when a multiple choice option has reached it's "cap".
Question 1 Example)
A student registers for “tie-tying” in workshop 1, and gets taken to a new page. The Script edits the available choices on that new page based on the student’s first choice, and removes “tie-tying” from the list of possible choices on that new page, so “etiquette” is their only remaining option.
Question 2 Example)
Students can either register for “tie-tying” or “etiquette”, both responses are initially available in the Google Form. 30 students take the survey, all 30 register for the “tie-tying” workshop. The Apps Script references the response spreadsheet, realizes the “tie-tying” workshop is full, then removes it from the Google Form's list of possible choices. Student 31 goes to register, and their only option is “etiquette”.
If my question has already been asked and answered (believe me, I did search!) I'd appreciate the redirection.
I believe we can achieve your second objective without too much difficulty and modify the form, based on the current state of response.
The approach is to
Create the form and associate it with a response spreadsheet
In that response spreadsheet, create a script with a function (updateForm for instance)
Bind that function with the onFormSubmit event, see Using Container-Specific Installable Triggers.
Analyse the response in the updateForm function and modify your form using the Form Service
For instance
function updateForm(e) {
if (e.values[1] == 'Yes') {
Logger.log('Yes');
var existingForm = FormApp.openById('1jYHXD0TBYoKoRUI1mhY4j....yLWGE2vAm_Ux7Twk61c');
Logger.log(existingForm);
var item = existingForm.addMultipleChoiceItem();
item.setTitle('Do you prefer cats or dogs?')
.setChoices([
item.createChoice('Cats'),
item.createChoice('Dogs')
])
.showOtherOption(true);
}
}
When it comes to achieving the goal in your first question, its more delicate, as the form will not submit mid way. What is possible is to go to different pages based on different responses to a Multiple Choice question, your use case may fit this method, although its not very dynamic.
Further its possible to use html Service to create completely dynamic experience.
Let me know if you need further information.
You are not able to create this type of dynamic form using the Google Forms Service, because there is no interaction between the service and scripts during form entry, except upon Form Submission. In the case of a multi-page form, a script has no way to know that a student has completed one page and gone on to another.
You could achieve this using the HtmlService or UiService, though. In either case, you'd rely on the client-side form interacting through server-side scripts to get updated lists of course options, then modifying the next 'page'. It will be complex.
The other answer to this question will keep adding a multichoice select each time for the form is submitted. Using similar approach of:
Create the form and associate it with a response spreadsheet
In that response spreadsheet, create a script with a function (updateForm for instance)
Bind that function with the onFormSubmit event, see Using Container-Specific Installable Triggers.
Analyse the response in the updateForm function and modify your form using the Form Service
I've used the following code to modify a list select which could be easiliy modified for a multiple choice.
function updateForm(){
var form = FormApp.openById('YOUR_FORM_ID'); // Base form
// need to read what dates are available and which are taken
var doc = SpreadsheetApp.getActiveSpreadsheet();
var dates = doc.getRange("dates!A1:A10").getValues(); //available options
var taken_dates = doc.getRange("responses!F2:F51").getValues(); //just getting first 50 responses
// joining the taken dates into one string instead of an array to compare easier
var taken_dates_string = taken_dates.join("|");
var choice = [];
// loop through our available dates
for (d in dates){
// test if date still available
if (dates[d][0] != "" && taken_dates_string.indexOf(dates[d][0]) === -1){
choice.push(dates[d][0]); // if so we add to temp array
}
}
var formItems = form.getItems(FormApp.ItemType.LIST); // our form list items
// assumption that first select list is the one you want to change
// and we just rewrite all the options to ones that are free
formItems[0].asListItem().setChoiceValues(choice);
}
I have a script creating buttons in a table.
Each button is assigned to the same callback server function.
My question is how do I know which of these buttons triggered the server call?
Also is there a way of outputting a msgbox from the server script? When I try browser.msgBox, it gives an error saying that msgbox can't be used in this context.
var clientDel = app.createServerHandler("delStudent").addCallbackElement(grid);
table.setWidget(t, 4, app.createButton("Delete", clientDel).setId("del1"));
function delStudent(e){
var app = UiApp.getActiveApplication();
e.
return app;
}
Thanks,
C Singh.
simply e.parameter.source will return the ID of the widget that originates the handler function
You can use a label or a textBox that you create in your UI and set invisible. Then at any moment you can use getElementById(label_ID').setVisible(true) and put any text in it.
If you want it to appear at a specific position above all other widgets you can do that quite simply using setStyleAttributes, see this other post(server version) for further info and practical example