Extending a google spreadsheet into a google web app - javascript

I was coding using the google scripts, when I came across a problem I've been struggling with for a couple days now. I am using Code.gs (a default page in creating a web app in google), when I called in data from a google spreadsheet to try and display it on a webpage. I had no problems with calling in the data add storing it into a array but now I am struggling with trying to return it to my javascript code. Can this be done or is there something else I can do to fix it? My code is below.
function getContents()
{
var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xum5t4a83CjoU4EfGd50f4ef885F00d0erAvUYX0JAU/edit#gid=0&vpid=A1');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
var education = [];
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
if(j==1){
education[education.length] = values[i][j];
}
}
}
}
Logger.log(education);
return education;
}
From that Code.gs code i want it to return it to a Javascript function that says this:
function onNew(){
var input = google.script.run.getContents();
for(var = 0; i<input.length; i++)
{
$("#main").append("<div id='class'>"+input[i]+"</div>);
}
}
and whenever I try to run it says that it causes an error because it is undefined. Thanks in advance! Anything helps!

You need to use the withSuccessHandler(). Your variable input will not receive the return from google.script.run.getContents()
Separate out the client side code into two functions:
HTML Script
function onNew() {
google.script.run
.withSuccessHandler(appendEducationHTML)
.getContents();
};
function appendEducationHTML(returnedInfo) {
console.log('returnedInfo type is: ' + typeof returnedInfo);
console.log('returnedInfo: ' + returnedInfo);
//If return is a string. Convert it back into an array
//returnedInfo = returnedInfo.split(",");
for (var = 0;i < returnedInfo.length; i++) {
$("#main").append("<div id='class'>"+returnedInfo[i]+"</div>);
};
};

Related

JavaScript - push results from for loop into array (with SoundCloud API)

I have this function that iterates through a user's favourited track genres on SoundCloud, and then I want to push these results into an array however am having problems. Any suggestions?
SC.connect(function() {
SC.get('/me/favorites', function(favorites) {
console.log(favorites)
for (i = 0; i < favorites.length; i++) {
console.log(favorites[i].genre);
favorites[i].genre.push(meGenrePref);
}
var meGenrePref = [];
console.log(meGenrePref);
});
});
I'm not expert on SoundCloud APIs but watching your code I thought this solution:
var meGenrePref = [];
SC.connect(function(){
SC.get('/me/favorites',function(favorites) {
console.log(favorites);
for(var i = 0; i < favorites.length; i++)
{
console.log(favorites[i].genre);
meGenrePref.push(favorites[i].genre);
}
console.log(meGenrePref);
});
});
You were trying to push the contents to a wrong variable, which was the response from the API.
In this solution we are saving to meGenrePref all genres.

Ember.JS do an operation every time the JSON is loaded

I am new to Ember and in my APP I need to add some operations depending on the JSON I get from the server.
This is currently my logic:
In the component.js
var foo = [];
didInsertElement() {
var data = this.get('content');
//then I do some transformations with data to obtain what I want and assign it to foo
data.forEach(function(option) {
list.push( JSON.parse(JSON.stringify(option)))
})
for(i = 0; i < list.length; i++) {
//some logic code with list
}
this.set('foo', someTransformationFromList);
for (i = 0; i < count; i++) {
this.get('content').push(jQuery.extend(true, {}, this.get('content')[0]));
}
for(i = 0; i < foo.length; i++) {
this.get('content')[i].set('string1', foo[i].text);
this.get('content')[i].set('id', foo[i].value);
}
}
So now the question is, I need to move this logic from didInsertElement to somewhere else so it gets executed every time I get my JSON no just the first time when the component is rendered. I have tried to use a serializer or a transform but I don't know if I can use any of them. Can you please give me any pointers about how to do this task?.
Thank you.

How to search a cell in Google Sheets that has multiple words for a specific word and return a result?

Here's what I'm trying to accomplish:
A project manager fills out a Google Form. It spits the result to a spreadsheet, and I want to manipulate that data.
The first question/cell is what social media the product is using, with a multiple choice checkbox.
If someone checks off Facebook and Twitter, the cell returns "Facebook, Twitter" and I'm having trouble searching the cell for "Twitter" since it's not first.
Here's what I have so far:
function testplan() {
var mainsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var testplan = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestPlan");
var socialData = mainsheet.getRange(3, 3).getValues();
var socialDataText = socialData.length;
var fbSocial = "Facebook";
var twitSocial = "Twitter";
for (var i = 0; i < socialDataText; i++) {
if (socialData[i][0] == fbSocial) {
testplan.getRange("A15").setValue("User can log into the app with Facebook.");
testplan.getRange("A16").setValue("User can share content from the app with Facebook.");
} {
return 0;
};
};
for (var i = 0; i < socialDataText; i++) {
if (socialData[i][0] == twitSocial) {;
testplan.getRange("A17").setValue("User can log into the app with Twitter.");
testplan.getRange("A18").setValue("User can share content from the app with Twitter.");
} {
return 0;
};
};
The first command to look for "Facebook" works, because it's the first bit of text. But anything after it isn't able to be found. I've searched a lot and have found things that are close, but nothing that works the way I'm expecting it to. It's probably a command or something really obvious that I'm not seeing.
try
function testplan() {
var mainsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var testplan = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestPlan");
var socialData = mainsheet.getRange(3, 3).getValues();
var socialDataText = socialData.length;
var fbSocial = "Facebook";
var twitSocial = "Twitter";
for (var i = 0; i < socialDataText; i++) {
if (socialData[i][0].indexOf(fbSocial) > -1) {
testplan.getRange("A15").setValue("User can log into the app with Facebook.");
testplan.getRange("A16").setValue("User can share content from the app with Facebook.");
};
if (socialData[i][0].indexOf(twitSocial) > -1) {;
testplan.getRange("A17").setValue("User can log into the app with Twitter.");
testplan.getRange("A18").setValue("User can share content from the app with Twitter.");
};
};
};
for simplicity

Need Help to adding new array into JSON object

I'm making an file managing web page with nodejs and need some help.
I'm adding some informations into JSON object which recieved from DB.
the DB constains stage informations and I should link informations about stage files.
this is code for the function. Added comments into codes.
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
// this is a problem. I will add file information for each file.
// I
for (var j = 0; j < data.stage[i].packFileNameArray.length; j++) {
fs.stat(dirPath + data.stage[i].packFileNameArray[j], function (err, fileStat) {
if (err) {
// this was first try. I thought the array will be automaticallt created.
data.stage[i].isExist[j] = 'NO';
data.stage[i].mTime[j] = '0';
data.stage[i].size[j] = '0';
} else {
// this was second try. I tried push into each time.
data.stage[i].isExist.push('YES');
data.stage[i].mTime.push(fileStat.mTime);
data.stage[i].size.push(fileStat.size);
}
console.log(data.stage[i].isExist[j]);
console.log(data.stage[i].mTime[j]);
console.log(data.stage[i].size[j]);
});
}
}
});
I want to know how I can add additional informations as an array into the JSON object.
Thank you.
data.stage[i].push({ isExist: 'Yes'});
etc
You have to create the arrays (as the do not seem to exist) before you can push data into them:
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
data.stage[i].isExist = [];
data.stage[i].mTime = [];
data.stage[i].size = [];
//...

JavaScript Find Specific URLs.

Alright, I've been working on a userscript that redirects when a specific page is loaded.
This is what I have so far:
function blockThreadAccess() {
var TopicLink = "http://www.ex.com/Forum/Post.aspx?ID=";
var Topics = [ '57704768', '53496466', '65184688', '41182608', '54037954', '53952944', '8752587', '47171796', '59564382', '59564546', '2247451', '9772680', '5118578', '529641', '63028895', '22916333', '521121', '54646501', '36320226', '54337031' ];
for(var i = 0; i < Topics.length; i++) {
if(window.location.href == TopicLink + Topics[i]) {
// Execute Code
}
}
}
The function is called on the page load, but it doesn't seem execute the code.
What it's supposed to do is check to see if the user is on that specific page, and if he is then execute the code.
Say someone goes to this link - http://www.ex.com/Forum/Post.aspx?ID=54646501, it then redirects the use. I'm trying to make it efficient so I don't have to add a bunch of if statements.
try converting both to lower case before comparing
var loc = window.location.href.toLowerCase();
var topicLnk = TopicLink.toLowerCase();
for(var i = 0; i < Topics.length; i++) {
if(topicLnk + Topics[i] == loc) {
// Execute Code
}
}

Categories

Resources