populating dropdown list values from google spreadsheet using HTML service - html

Hi I am trying to get a dropdown list using html and google script.
The values for the list should be populated from a google spreadsheet. After searching a lot on stackoverflow I managed the below code. But I am still not able to get the data from spreadsheet as dropdown option. There is no error but i am just getting a blank dropbox as output
Spreadsheet link:https://docs.google.com/spreadsheets/d/1qaxzTWRf4_xDhfkf-_S9Zr2m-M-9blOcpFBt4JyT5do/edit#gid=0
code.gs
function doGet() {
var template = HtmlService.createTemplateFromFile('Index');
var htmlOutput = template.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
return htmlOutput;
}
html code
< select id = "something" > < option > Choose a option < /option>
</select > < body onload = "addList()" > < /body>
<script>
function addList(){
var sheet = SpreadsheetApp.openById('1qaxzTWRf4_xDhfkf-_S9Zr2m-M-9blOcpFBt4JyT5do').getSheetByName('Sheet1');
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("C2:C" + lastRow);
var options = new Array()
var select = document.getElementById("something");
var data = myRange.getValues();
for(var i = 0; i < lastRow; i++) {
options.push(data[i]);
}
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
var el = document
el.text = opt;
el.value = opt;
select.appendChild(el);
}
</script >

Related

Closed: vanishing data in Google Form after refreshing the page

I wrote a google script, which is creating form, using data from google spreadsheet. I'm using drop-down answer options. The code works normally, all data appears, but sometimes ( 3 times of 5 attempts), after refreshing browser page, some questions disappeared. Everytime it's different question, so it isn't problem with specific question.
What is the problem and how can i fix it?
UPD: Solved. Problem was in opened Google Form while code was compiled.
function myFunk(){
var SHEET_ID = FormApp.getActiveForm().getDestinationId();
var ss = SpreadsheetApp.openById(SHEET_ID);
var form = FormApp.getActiveForm();
var sheets = ss.getSheets();
var numb = sheets[1].getLastRow();
var TypeSelect = form.addListItem()
.setRequired(true);
var TypeChoices = getData(TypeSelect,sheets[1],1,numb)
TypeSelect.setChoices(TypeChoices);
var ClientSelect = form.addListItem()
.setRequired(true)
var ClientChoices = getData(ClientSelect, sheets[1],2,numb)
ClientSelect.setChoices(ClientChoices);
var phases = ["Start","End"];
var PhasesChoices=[];
var PhaseSelect = form.addMultipleChoiceItem()
.setRequired(true);
for (var t=0;t<phases.length; t++){
PhasesChoices.push(PhaseSelect.createChoice(phases[t]));
}
PhaseSelect.setChoiceValues(phases);
}
function getData(DataSelect,sheet,numbColumn,lRow){
var DataChoices = [];
var DataValues = sheet.getRange(2,numbColumn,lRow,1).getValues()
.filter(CheckFull);
var data = [];
for(var i = 0; i < DataValues.length; i++) {
data.push(DataValues[i]);
}
for(var j =0; j < data.length; j++) {
DataChoices.push(DataSelect.createChoice(data[j]));
}
return DataChoices;
}
function CheckFull(value){
return value != '';
}

Google Forms Dynamic multiple choice with pictures

I have a dynamic multiple choice google form that is working fine except for if i add a picture to the option box using the attach picture function in the google form editing screen.
When the form refreshes all the pictures are lost.
below is the code for the dynamic form.
What i need is to be able to assign a picture the the option title.
function updateLists() {
var files = DriveApp.getFolderById("0B-yHzwrw6kldVDVzZlA5bTlScDg").getFiles()
while (files.hasNext()) {
var file = files.next();
var form = FormApp.openById(file.getId())
var items = form.getItems();
for (var i = 0; i < items.length; i += 1){
var item = items[i]
if (item.getTitle() === "Requested Award"){
var agentList = item.asMultipleChoiceItem()
}
}
var ss = SpreadsheetApp.getActive().getSheetByName("Menu");
var agentValues = ss.getRange(1, 15, ss.getMaxRows() - 1).getValues();
var agentNames = [];
for(var i = 0; i < agentValues.length; i++)
if(agentValues[i][0] != "")
agentNames[i] = agentValues[i][0];
agentList.setChoiceValues(agentNames);
}
}

google script maximum execution time - return google group emails (huge list)

Im trying to save google groups emails into a spreadsheet but Im getting the 'maximum execution time' error. Any ideas?
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
var sheet = SpreadsheetApp.create("Group Mail");
for (var i = 0; i < users.length; i++) {
sheet.appendRow([users[i]]);
}
}
What is probably taking much of the time is the appendRow() call.
you should build an array with all the values and write this at once in your sheet.
Code could be like this :
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
var sheet = SpreadsheetApp.create("Group Mail");
var values = [];
for (var i = 0; i < users.length; i++) {
values.push([users[i]]);
}
sheet.getRange(1,1,values.length, values[0].length).setValues(values);
}
EDIT
I didn't check the begining of your initial code, SpreadsheetApp.create("Group Mail"); returns a spreadsheet, not a sheet... that's why it fails on getRange.
Since it's not clear what you wanted to get exactly I assumed you wanted to create a new Sheet with that name if it doesn't exist.
The appropriate code would be like this :
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
if(SpreadsheetApp.getActive().getSheetByName("Group Mail") == null){
var sheet = SpreadsheetApp.getActive().insertSheet("Group Mail");
}else{
var sheet = SpreadsheetApp.getActive().getSheetByName("Group Mail");
}
var values = [];
for (var i = 0; i < users.length; i++) {
values.push([users[i]]);
}
sheet.getRange(1,1,values.length, values[0].length).setValues(values);
}

Stop recording to spreadsheet if Gmail Label exist

I like to record an email's Subject/Sender/Date/ID into a spreadsheet by it's label and add a "Recorded" label to the email once it's recorded into the spreadsheet. I like to run the script again without re-recording emails with the label "Recorded"
I can get it to the point of recording to spreadsheet and adding the label. but everything i run the script it grabs everything again.
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var searchlabel = GmailApp.getUserLabelByName("Test");
var newlabel = GmailApp.getUserLabelByName("Recored");
var threads = searchlabel.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getBody();
var sub = messages[j].getSubject();
var from = messages[j].getFrom();
var dat = messages[j].getDate();
var id = messages[j].getId();
var current = new Date();
ss.appendRow([current, dat, sub, id, from])
}
threads[i].addLabel(newlabel);
}
}
I've search the forum and everything tends to suggest to remove the existing label and rename to a different label. But what i wantenter code here is to keep the existing label.
You should the search method since it lets you filter emails that belong to a particular label. Here's the modified version.
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var searchlabel = GmailApp.getUserLabelByName("Test");
var recordlabel = GmailApp.getUserLabelByName("Recorded");
var threads = GmailApp.search("label:Test -label:Recorded");
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getBody();
var sub = messages[j].getSubject();
var from = messages[j].getFrom();
var dat = messages[j].getDate();
var id = messages[j].getId();
var current = new Date();
ss.appendRow([current, dat, sub, id, from])
}
threads[i].addLabel(recordlabel);
}
}
You can try replacing var threads with:
var threads = GmailApp.search('label:(Test -Recored)');
This searches for all labels with Test but not Recored.
You wouldn't need var searchlabel anymore.

Google Docs - Scripts, copy cell from one sheet to another sheet

I need to copy a cell from a sheet to corresponding cell in another sheet
From here:
to here:
My sheet can be viewed here.
Please report back if this is not achieving what you require.
function updateVerified() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange('Calculates!D5:K').getValues();
var respSheet = ss.getSheetByName('Responses');
var respTs = respSheet.getRange('A2:A').getValues();
var respRange = respSheet.getRange('F2:F');
var respValues = respRange.getValues();
var convTs = [], temp;
for (var i = 0; i < respTs.length; i++) {
convTs[i] = respTs[i][0] ? respTs[i][0].getTime() : '';
}
for (var j = 0; j < source.length; j++) {
if (!source[j][0]) break;
temp = convTs.indexOf(source[j][0].getTime());
if (temp > -1) respValues[temp][0] = source[j][7];
}
respRange.setValues(respValues);
}