Retrieving google form response data - google-apps-script

I have a google form, where I need to use item ID and get all the response for that Item.
I have the below script which will timeout if the form has more than 3000 responses, as its inefficient
How Do I optimize it to retrieve all the items in a short span of time
fO.items = ["ItemID1","ItemID2","ItemID3"...];
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
var otherItems = '';
var flag = true;
for (var j = 0; j < fO.items.length; j++) {
var item = form.getItemById(parseInt(fO.items[j]));
if (response.getResponseForItem(item))
var otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
}

Currently, your code is getting the item object with the following line:
var item = form.getItemById(parseInt(fO.items[j]));
So, that line of code is reading the Form many times.
You could try getting the item objects once, putting them into a JSON object, and then retrieving them as needed.
I haven't tested this code, and I don't know if it will work, or if it will be faster if it does work. But thought I'd share the idea.
function getSomeAnswers() {
var form,flag,i,item,itemID,itemList,itemsObject,
k,L,L_items,otherItems,responses,response,thisAnswer;
itemList = ["ItemID1","ItemID2","ItemID3"];
form = FormApp.getActiveForm();
responses = FormApp.getActiveForm().getResponses();
itemsObject = {};
L_items = itemList.length;
for (i = 0; i < L; i++) {//Compile a list of item objects
itemID = parseInt(itemList[i]);
itemsObject[itemID] = form.getItemById(itemID);
}
L = responses.length;
for (i = 0; i < L; i++) {
response = responses[i];
otherItems = '';
flag = true;
for (k in itemsObject) {//Loop through every item to get
item = itemsObject[k];
thisAnswer = response.getResponseForItem(item);
Logger.log(thisAnswer)
if (thisAnswer)
otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
/*
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
*/
}
}

Related

use .removeFromThreads in more than 100 threads inside a google app script

i have a script that removes every thread within a label and all sublabels.This was workng fine, but recently i got an error that says that the operation childrens[i].removeFromThreads(threads); can not be applied to more that 100 threads... How can i fix this?
function removingThreadsfromLabel() {
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
childrens[i].removeFromThreads(threads);
}
}
function children(parent) {
var name = parent.getName() + '/';
return GmailApp.getUserLabels().filter(function(label) {
return label.getName().slice(0, name.length) == name;
});
}
For now i have done the following, but it is not optimal...
function removingThreadsfromLabel() {
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
Logger.log(threads.length);
while (threads.length>100){
childrens[i].removeFromThread(threads[0]);
var threads = childrens[i].getThreads();
}
childrens[i].removeFromThreads(threads);
}
}
Regards,
Ok, i figure out how to do it in batch, so it is better that my first solution:
function removingThreadsfromLabel() {
var batchSize = 100;
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
Logger.log(threads.length);
for (var j = 0; j < threads.length; j+=batchSize) {
childrens[i].removeFromThreads(threads.slice(j, j+batchSize));
}
var threads = childrens[i].getThreads();
childrens[i].removeFromThreads(threads);
}

Issue with assigning Google Map Autocomplete results to variables

I have issue with with this line:
var user-city = addressObj[j].long_name;
I see proper value in console log but can't assign it to the variable.
function onPlaceChanged() {
var places = autocomplete.getPlace();
//console.log(places);
// var place = places[0];
console.log(places);
console.log(places.address_components);
user_home_address = places.name;
for(var i = 0; i < places.address_components.length; i += 1) {
var addressObj = places.address_components[i];
for(var j = 0; j < addressObj.types.length; j += 1) {
if (addressObj.types[j] === 'locality') {
// console.log(addressObj.types[j]); // confirm that this is 'city'
var user-city = addressObj[j].long_name;
// console.log(addressObj.long_name); // confirm that this is the city name
}
}
for(var j = 0; j < addressObj.types.length; j += 1) {
if (addressObj.types[j] === 'administrative_area_level_1') {
// console.log(addressObj.types[j]); // confirm that this is 'province'
// console.log(addressObj.short_name); // confirm that this is the province name
}
}
}

Cleaner way to get a key and value from JSON

Currently the way that I am taking a JSON string where I "Don't know the contents" and pulling the keys and the values is as follows:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var arrLen = arr.length;
for(var i = 0; i < arrLen; i++){
var myKeys = Object.keys(arr[i]);
var keysLen = myKeys.length;
for(var x = 0; x < keysLen; x++){
keyName = myKeys[x];
keyValueStr = "arr[i]."+keyName;
keyValue = eval(keyValueStr);
console.log(keyName+':'+keyValue);
}
}
There has to be a cleaner and more efficient way of doing this. Any suggestion would be appreciated.
Using jQuery you can use http://api.jquery.com/jQuery.parseJSON/ & Object.keys - Than:
var obj = jQuery.parseJSON('{"manager_first_name":"jim","manager_last_name":"gaffigan"}');
var keys = Object.keys(obj);
jQuery.each(keys,function(k, v){
console.log(v);
console.log(obj[v]);
});
Create an object and initialize it with your JSON:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var JSONObject = new MyJSONObject(arr);
in your constructor, set the object's properties.
You could use for-in loop which iterates over the enumerable properties of an object, in arbitrary order.
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
for (var i = 0; i < arr.length; i++) {
var currentObj = arr[i];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}
}
If your array always have only one item, you could omit the outermost for loop and just use arr[0]
var currentObj = arr[0];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}

Making comparisson to items in an Object

I have the following object:
var users:Object= new Object();
users[0]["user_id"] = "1124";
users[0]["name"] = "ikke";
users[0]["age"] = "24";
users[0]["gender"] = "male";
users[1]["user_id"] = "1318";
users[1]["name"] = "test";
users[1]["age"] = "20";
users[1]["gender"] = "male";
var selectors:Object = new Object();
selectors["user_id"] = 1318;
selectors["gender"] = "male";
what i want is to use the selectors object in an if statement. In humans lanuguage it should be something like:
for (var index:String in users) {
If users[index]["gender"] == selectors[gender] && users[index]["user_id"] == "male" -> then trace "success".
}
The tricky part is that the selectors object is dynamic. Sometimes it can contain only 1 item , sometimes 3 items. Or it can also be null. In that case it should allways trace success. Anyone that can help me?
for(var i:int = 0; i < users.length; i++) {
var success:Boolean = true;
for(var key:String in selectors) {
if(users[i][key] != selectors[key]) {
success = false;
break;
}
}
if(success) {
trace('success for user ' + i);
}
}

How to add data dynamically in JSON object

Sample code
var mydata = [ {id:"1",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, {id:"2",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}];
I have to get output something like above, have to add dynamically columns and its value to mydata.
I am trying to do something like this but its not working for me.
var mydata = [];
for (var r = 0; r < dataTable.getNumberOfRows(); r++) {
var items = "";
var y ="";
for (var c = 0; c < dataTable.getNumberOfColumns(); c++) {
items += '"'+dataTable.getColumnLabel(c)+'":'+dataTable.getValue(r, c)
if(c!=dataTable.getNumberOfColumns()-1){
items += ",";
}
}
y = "{"+items+"}";
mydata.push(y);
}
above code doesnt works for me. any other way for it
Easy:
oldJsonObj.vector = [] //this creates a new element into the object
foreach(dataTable.getNumberOfRows()){
var x = {}
x.id = XXX;
x.name = XXX;
oldJsonObj.vector.push(x); //adds the element x to array
}
alert(oldJsonObj.vector[i].name); //easy accses
Hope this can help you:
var json = [];
// add an field 'a'
json['a'] = 1;
alert(json["a"]); //1
// add an field 'b'
json['b'] = []; // another json object
...
in your case:
mydata=[];
// add json fields
mydata["id"] = 1;
mydata["invdate"] = "2007-10-01";
mydata["name"] = "test";
//...