使用谷歌脚本将电子表格中的值存储在对象中

时间:2015-01-22 23:18:28

标签: google-apps-script google-sheets google-apps

我正在尝试编写一个程序,该程序将从电子表格中获取条目,并将从每行中的单元格中获取的数据存储为对象,以便稍后可以使用这些条目。

虽然我在程序中找到文档中的数据时遇到了一些麻烦,但是我一直无法使程序存储它在新对象中检索的数据,以便以后可以在程序中使用所述数据。 / p>

当我尝试让程序打印从文档上的电子表格中获取的内容时,它只返回undefined(注意:为了尝试创建该项目的工作原型。我只处理一个条目目前。我对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);
}

任何有用的东西都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

试试这个:

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);
}

答案 1 :(得分:0)

这一行:

var x = new entry(name1, name2, choice1, choice2, choice3);

正在创建一个名为“x”的对象,但没有任何东西被放入对象中。

entry函数没有返回任何内容。您需要一个return语句将对象返回到“x”。

请注意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;
};