I am trying to write a program that will take entries from a spreadsheet and store data taken from the cells in each row as an object, such that the entries can be used later.
While I have had little trouble getting the program to find data in the document, I have been having trouble making the program store the data that it retrieves in a new object so that said data can be used later in the program.
when I try to have the program print what it has taken from the spreadsheet on a document, it just returns undefined (note: for the purposes of trying to create a working prototype of this project. I am only dealing with one entry currently. I am also very new to Google script,. so apologies if I have made any terrible errors).
var one =[];
var two =[];
var three =[];
var four =[];
var five =[];
var sortData = function(startRow, endRow){
//var sortedSheet = SpreadshetApp.create("sorted");
function entry(firstname, lastname, firstchoice, secondchoice, thirdchoice){
this.firstName = firstname;
this.lastName = lastname;
this.firstChoice = firstchoice;
this.secondChoice = secondchoice;
this.thirdChoice = thirdchoice;
};
for(x = startRow;x <= endRow;x++){
var sheet = SpreadsheetApp.getActiveSheet();
var name1 = sheet.getRange("B"+x.toFixed(0)).getValue();
var name2 = sheet.getRange("C"+x.toFixed(0)).getValue();
var choice1 = sheet.getRange("D"+x.toFixed(0)).getValue();
var choice2 = sheet.getRange("E"+x.toFixed(0)).getValue();
var choice3 = sheet.getRange("F"+x.toFixed(0)).getValue();
var entries = DocumentApp.create("this code works");
var x = new entry(name1, name2, choice1, choice2, choice3);
switch(x.firstChoice.toFixed(1)){
case "1.0":
one[one.length] = this;
entries.getBody().appendParagraph(one[0].firstChoice);
break;
case "2.0":
two[two.length] = this;
entries.getBody().appendParagraph(two[0].firstChoice);
break;
case "3.0":
three[three.length] = this;
entries.getBody().appendParagraph(three[0].firstChoice);
break;
case "4.0":
four[four.length] = this;
entries.getBody().appendParagraph(four[0].firstChoice);
break;
case "5.0":
five[five.length] = this;
entries.getBody().appendParagraph(five[0].firstChoice);
break;
default:
Logger.log("this code is not working");
}
}
}
function onOpen(){
var current = SpreadsheetApp.openById("1uHWBHeqnl18pDDxbSJj2_WyTXxaT96UfRg4oZEF7uHc");
Logger.log(current.getName());
SpreadsheetApp.setActiveSpreadsheet(current);
sortData(2,2);
}
Anything that helps would be appreciated.
Try this:
function getValsFromSheet(startRow, endRow) {
if(!startRow){ startRow = 1}
if(!endRow){ endRow = 2}
var as = SpreadsheetApp.getActiveSpreadsheet();
var sheet = as.getActiveSheet();
var numOfRows = endRow - startRow+1;
var rowData = sheet.getRange(startRow,1,numOfRows,2).getValues();
var rowDataAsString = JSON.stringify(rowData);
var properties = PropertiesService.getDocumentProperties();
properties.setProperty("storedValue", rowDataAsString);
}
function getValsFromProps(){
var properties = PropertiesService.getDocumentProperties();
var storedValueString = properties.getProperty("storedValue");
var storedValue = JSON.parse(storedValueString);
Logger.log(storedValue);
}
This line:
var x = new entry(name1, name2, choice1, choice2, choice3);
Is creating an object named "x", but nothing is getting put into the object.
The entry
function is not returning anything. You need a return statement to return the object into "x".
Note the return statement:
function entry(firstname, lastname, firstchoice, secondchoice, thirdchoice) {
Logger.log('firstchoice: ' + firstchoice);
this.firstName = firstname;
this.lastName = lastname;
this.firstChoice = firstchoice;
Logger.log('theObject.firstChoice: ' + this.firstChoice);
this.secondChoice = secondchoice;
this.thirdChoice = thirdchoice;
return this;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.