copyValuesToRange

时间:2015-11-03 23:45:14

标签: google-apps-script google-sheets

我正在尝试使用具有日期加上字段的表单输入,该字段指示范围中的天数。日期在表单上输入为mm/dd/yyyy,天数为1-7的数字。

我正在使用表单电子表格中的新行并将其复制N次,N是该范围内的天数。

然后我尝试通过在字段中添加一天来修改每个重复记录中的一个字段,该字段是YYYY-MM-DD格式的日期。当我尝试覆盖我得到的一个字段时:

  

TypeError:在对象2015-11-05中找不到函数copyValuesToRange。(第46行,文件“”)

我使用的代码是我通过此论坛找到的一些代码和一些修改:

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 个答案:

答案 0 :(得分:0)

正如Samantha所解释的那样,你可以使用.copyRange()函数来替代.copyRalgeToRange()。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);

希望这会转化为你正在尝试做的事情!如果没有,请问我,也许通过一些公开的讨论我们可以一起弄清楚如何将它应用到您的数据。