2017-05-18 33 views
0

因此,我正在從電子表格中獲取數據,將相關部分放入數組中並將其寫回新的電子表格。數據來自谷歌形式,並不是每個人都有對每個問題的輸入,所以我只接受非空的問題。這導致我的數組的長度變化谷歌腳本insertSheet不接受字符串對象?

所以我已經把它放到了最親密的步驟,我被卡在插入工作表的部分,它在1的迭代中工作正常,但是當我在循環中執行它是有錯誤的。

for (j=2; j <= 3 ; j++)// I have 23 iterations, but I stopped at 2 for now 
    { 

     var cellLocation = new String ("B" + j + ":AC" + j); 
     Logger.log("J is " + j + "Cell location " + cellLocation); 
     var workingRange = buySheet.getRange(cellLocation); 
     var customer = workingRange.getValues(); //this produces a range of the row 
     Logger.log("Range is " + workingRange.getA1Notation() + " data is " + customer[0][0]); 
    //At first I thought it was the range- so I broke everything out into the tiniest step 

    var nameCustomer = new String(customer[0][0]); 
    Logger.log("nameCustomer is " + nameCustomer); 
    //this correctly prints out the names as strings- through 23 iterations 
    var create = buySheet.insertSheet(nameCustomer); 
    //this is a sheet object with the customers name- fails after the first iteration, and creates sheets with blank names - 
} 

這是我使用的參考:https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#insertSheet(String)

我註釋掉了一切後,這對解決這一棘手的問題。我試過在循環外實例化字符串。我可以發佈日誌,我使用的是真實數據,所以我不得不模糊人的名字。

它適用於第一輪,它工作,如果我手動鍵入一個字符串。

回答

0

我想通了!當我創建新工作表時,它將活動工作表移至新工作表。因此,在循環結束時,我必須將活動工作表重置爲第一個工作表所在的數據。

回想起來似乎很明顯。

相關問題