转到Google电子表格中的顶部功能

时间:2014-08-24 12:34:43

标签: excel google-apps-script google-sheets

此功能采用当前选定的单元格或范围,并将其移动到“列表”的顶部,可以这么说。在待办事项清单中非常有用。感谢Brian P的帮助!

function arrayShift(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var activeRange = sheet.getActiveRange();
  var toDoRange = sheet.getRange("A2:A9"); //the range of your todo list. 
  var howMany = activeRange.getNumRows();
  var topCell = activeRange.getRow();
  var rangeTop = toDoRange.getRow();
  var index = topCell - rangeTop; //gives the index of the selected cell
  var rangeArray = toDoRange.getValues();
  rangeArray.splice(index, howMany)

  if ( howMany == 1 ) {
    rangeArray.unshift(activeRange.getValues()[0]); 
  } else { // handle re-inserting multiple row values
    var values = activeRange.getValues();
    for ( var i = values.length - 1 ; i >= 0; i--) {
      rangeArray.unshift(values[i]);
    }
  }
  //Logger.log(rangeArray)
  toDoRange.clearContent();
  toDoRange.offset(0,0,rangeArray.length).setValues(rangeArray);
};

1 个答案:

答案 0 :(得分:1)

尝试...

function arrayShift(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var activeRange = sheet.getActiveRange();
  var toDoRange = sheet.getRange("A2:A9"); //the range of your todo list. 
  var howMany = activeRange.getNumRows();
  var topCell = activeRange.getRow();
  var rangeTop = toDoRange.getRow();
  var index = topCell - rangeTop; //gives the index of the selected cell
  var rangeArray = toDoRange.getValues();
  rangeArray.splice(index, howMany)

  if ( howMany == 1 ) {
    rangeArray.unshift(activeRange.getValues()[0]); // "remove brackets"
  } else { // handle re-inserting multiple row values
    var values = activeRange.getValues();
    for ( var i = values.length - 1 ; i >= 0; i--) {
      rangeArray.unshift(values[i]);
    }
  }
  //Logger.log(rangeArray)
  toDoRange.clearContent();
  toDoRange.offset(0,0,rangeArray.length).setValues(rangeArray);
};