簡體   English   中英

使用Google腳本將電子表格中的值存儲在對象中

[英]storing values from a spreadsheet in an object with google scripts

我正在嘗試編寫一個程序,該程序將從電子表格中獲取條目,並將從每一行的單元格中獲取的數據存儲為一個對象,以便以后可以使用這些條目。

雖然讓我的程序在文檔中查找數據幾乎沒有什么麻煩,但我卻使程序難以將它檢索到的數據存儲在新對象中,以便以后在程序中使用該數據。

當我嘗試讓程序打印從文檔中的電子表格獲取的內容時,它只會返回未定義的值(請注意:出於試圖創建該項目的工作原型的目的。我目前僅處理一個條目。對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);
}

任何有幫助的將不勝感激。

嘗試這個:

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

這行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM