2015-11-03 54 views
1

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

我不是确定为什么我得到错误。

+1

您正在用线43上的日期对象覆盖Val4。 –

+0

@RobinGertenbach一个字符串,实际上是'Utilities.formatDate()'的输出。但无论如何,Val4是**不是** Range对象。 – Mogsdad

+0

关于如何修复它的任何想法? – Grinton

回答

0

由于Samantha解释,你可以改用.copyValuesToRange()函数.getRange()的。setValues方法()

因为我不完全理解你拉的范围,我会告诉你当我遇到同样的问题时,我最终做了什么。 我试图从一个工作表复制标题到另一个:

 // First define the sheet that you're pulling data from ("dataSheet") 

    var file = SpreadsheetApp.getActiveSpreadsheet(); 
    var dataSheet = file.getSheetByName("name"); 

    // Then define the new sheet that you're pulling data to, 
    // in my case I look to see if the secondary sheet ("updateSheet") 
    // already exists, otherwise I insert a new sheet, 
    // and define its name. 

    var updateSheet = file.getSheetByName("Update Data from " + name); 

    var updateSheet = updateSheet || file.insertSheet(); 
    updateSheet.setName("Update Data from " + name); 

    // Then I was looking to copy the headers of my first sheet 
    // and copy those to my second sheet. 

    var shortHeaders = dataSheet.getRange('A1:M1'); 
    var headersValues = shortHeaders.getValues(); 

    updateSheet.getRange("A1:M1").setValues(headersValues); 

希望这转换成你想要做的一样好东西!如果没有,请问我,也许有一些公开的讨论,我们可以一起弄清楚它如何应用到你的数据。