我想采取一个表单输入,有一个日期加上一个字段,指示一个范围内的天数。日期在表格中输入为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