2015-11-03 58 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); 

希望這轉換成你想要做的一樣好東西!如果沒有,請問我,也許有一些公開的討論,我們可以一起弄清楚它如何應用到你的數據。