我想採取一個表單輸入,有一個日期加上一個字段,指示一個範圍內的天數。日期在表格中輸入爲mm/dd/yyyy
,天數爲1-7之間的數字。錯誤與copyValuesToRange
我在窗體電子表格中將新行復制N次,N爲範圍中的天數。
然後,我試圖修改每個重複記錄中的一個字段,這是YYYY-MM-DD格式的日期,方法是向該字段添加一天。當我嘗試覆蓋一個領域,我得到:
TypeError: Cannot find function copyValuesToRange in object 2015-11-05.(line 46,file"")
我使用的代碼位和代碼段我已經通過這個論壇有一些修改發現:
function onSubmit() {
// Sheet to which form submits
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FormInput");
var lastRow = sheet.getLastRow();
// Determine the from date
var date = (sheet.getRange(lastRow, 4).getValue());
// READ THE NUMBER OF NEW ROWS FROM THE DESIGNATED COLUMN IN "FormInput"
var N = sheet.getRange(lastRow, 6).getValue();
// Sheet to which we will write
var sheetRec = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("UpdatedFormInput");
var lastColumn = sheetRec.getLastColumn();
var lastRowT = sheetRec.getLastRow();
var lastRowB = sheetRec.getLastRow();
// Create N new Rows by copying in the template row N times
var Template = sheetRec.getRange(2, 1, 1, lastColumn);
for (j = 1; j <=N; j++) {
Template.copyTo(sheetRec.getRange(lastRowT + j, 1));
}
var Val1 = sheet.getRange(lastRow, 1);
var Val2 = sheet.getRange(lastRow, 2);
var Val3 = sheet.getRange(lastRow, 3);
var Val4 = sheet.getRange(lastRow, 4);
var Val5 = sheet.getRange(lastRow, 5);
Val1.copyValuesToRange(sheetRec, 1, 1, lastRowT + 1, lastRowT + N);
Val2.copyValuesToRange(sheetRec, 2, 2, lastRowT + 1, lastRowT + N);
Val3.copyValuesToRange(sheetRec, 3, 3, lastRowT + 1, lastRowT + N);
Val4.copyValuesToRange(sheetRec, 4, 4, lastRowT + 1, lastRowT + N);
Val5.copyValuesToRange(sheetRec, 5, 5, lastRowT + 1, lastRowT + N);
// modify the date field in each of the new rows starting from the second new row
var startrowT = lastRowT + 2;
var endrowT = lastRowT + N;
// Copy new data values into the rows
for (j = 1; j <N; j++) {
var result = new Date(date.getTime()+j*(24*3600*1000));
Val4 = Utilities.formatDate(new Date(result), "GMT-5", "yyyy-MM-dd");
// I am trying to copy my new date, (current date + 1), into a cell in multiple rows, column 4
// the utility is returning a string and the receiving field is a date
// getting error "TypeError: Cannot find function copyValuesToRange in object 2015-11-05.(line 46,file"")
Val4.copyValuesToRange(sheetRec, 4, 4, startrowT, 1);
// getting error "TypeError: Cannot find function copyValuesToRange in object 2015-11-05.(line 46,file"")
startrowT = startrowT + 1;
}
}
我不是確定爲什麼我得到錯誤。
您正在用線43上的日期對象覆蓋Val4。 –
@RobinGertenbach一個字符串,實際上是'Utilities.formatDate()'的輸出。但無論如何,Val4是**不是** Range對象。 – Mogsdad
關於如何修復它的任何想法? – Grinton