[英]storing values from a spreadsheet in an object with google scripts
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). 当我尝试让程序打印从文档中的电子表格获取的内容时,它只会返回未定义的值(请注意:出于试图创建该项目的工作原型的目的。我目前仅处理一个条目。对Google脚本来说也是很新的。如果我犯了任何严重的错误,因此深表歉意。)
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. 正在创建一个名为“ x”的对象,但是没有任何内容放入该对象中。
The entry
function is not returning anything. entry
函数不返回任何东西。 You need a return statement to return the object into "x". 您需要一个return语句将对象返回到“ x”。
Note the return statement: 注意return语句:
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;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.