我自动更新了行列表,我想每天复制并保存它们。
我写了以下脚本:
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var rowcount = sheet.getRange("A1:A").getValues();
var sheetdef = ss.getSheets()[1];
for(var i = 0; i < rowcount.length; i++){
var weeknum = sheet.getRange('Feed!A1').getValue();
var year = sheet.getRange('Feed!B1').getValue();
var date = sheet.getRange('Feed!C1').getValue();
var sessions = sheet.getRange('Feed!D1').getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}
但是你会看到它永远不会停止运行,而且它只会复制第一行的数据。如果我能够识别我的问题,我真的不知道如何解决它。
我认为我的rowcount
行是错误的,因为它也算空行。我还需要更新sheet.getRange
公式,以便他们采用动态参数。
答案 0 :(得分:1)
未经测试但应该有效:
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
//var rowcount = sheet.getRange("A1:A").getValues();
var Avals = ss.getRange("A1:A").getValues();
var rowcount = Avals.filter(String).length; //This will take the last non blank cell from A column
var sheetdef = ss.getSheets()[1];
//I suggest you define Feed sheet as well
var feedsheet = ss.getSheetsByName("Feed")
for(var i = 0; i < rowcount.length; i++){
var weeknum = feedsheet(i,1).getValue(); //<- ith row and first/second column
var year = feedsheet.getRange(i,2).getValue();
var date = feedsheet.getRange(i,3).getValue();
var sessions = feedsheet.getRange(i,4).getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}
答案 1 :(得分:0)
在瓦西姆的帮助下,我想出了这个:
// function to save data Tendance sessions
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var sheetdef = ss.getSheets()[1];
lastRow = sheet.getLastRow();
for (i = lastRow; i > 0; i--){
var weeknum = sheet.getRange(i,1).getValue();
var year = sheet.getRange(i,2).getValue();
var date = sheet.getRange(i,3).getValue();
var sessions = sheet.getRange(i,4).getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}