Google表格:按索引

时间:2015-07-25 18:42:09

标签: google-apps-script google-sheets

我正在尝试使用计算索引获取工作表。我有一个问题但不确定是否将浮点数转换为整数或是否存在语法错误。我错了什么?

function copySheetValues()
{

  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = SpreadsheetApp.getActiveSheet();

  //get the source sheet data
  var sourceDataRange = sourceSheet.getDataRange();
  var sourceSheetValues = sourceDataRange.getValues();
  var sourceRows = sourceDataRange.getNumRows();
  var sourceColumns = sourceDataRange.getNumColumns();


  // get the source sheet index and set the next sheet index
  var sourcesheetIndex = sourceSheet.getIndex();
  var destinationsheetIndex = Math.round(sourcesheetIndex + 1);

  // get the next sheet
  var destinationSheet = spread.getSheets()[destinationsheetIndex]

  //destination.insertSheet(sourcename, 0);
  destinationSheet.getDataRange().offset(0, 0, sourceRows, sourceColumns).setValues(sourceSheetValues);

}

1 个答案:

答案 0 :(得分:1)

由于getIndex()基于1且getSheets()基于0,您可以尝试:

var destinationSheet = spread.getSheets()[sourceSheet.getIndex()];

去图

警告:getIndex()返回电子表格中的工作表位置,其中getSheets()与添加工作表的顺序有关,如果在添加工作表后重新安排电子表格,则两者可能不对应。 / p>

确保目标表是位于源表之后的工作表:

// get the source sheet index and set the next sheet index
  var destinationsheetIndex = sourceSheet.getIndex() + 1;

// get the next sheet
  var sheets = spread.getSheets();
  for (var i=0; i<sheets.length; i++) {
    if(sheets[i].getIndex() == destinationsheetIndex ) {
      var destinationSheet = sheets[i];
      break;
      };
    };