Google表格脚本 - 选择下一行+合并范围?

时间:2017-04-03 17:26:23

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

我正在尝试创建一个菜单列表,该列表将添加X行数,并且合并列H> M的新行添加(逐行 - 而不是整个块)。我无法解决如何选择活动行+1或活动行+2等,然后沿适当数量的单元格移动。

下面是我到目前为止用户界面并添加行的脚本,我将自己绑在一起试图合并我插入的新行的H> M!

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Planning Functions')
      .addItem('Add 1 Row', 'add1row')
  .addItem('Add 2 Rows', 'add2row')
      .addToUi();
}

function add1row() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var ar = ss.getActiveRange().getRow();
  ss.insertRowsAfter(ar, 1); 
}
function add2row() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var ar = ss.getActiveRange().getRow();
  ss.insertRowsAfter(ar, 2);
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Planning Functions')
      .addItem('Add 1 Row', 'add1row')
  .addItem('Add 2 Rows', 'add2row')
      .addToUi();
}

function add1row() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var ar = ss.getActiveRange().getRow();
  var mergeRange = sheet.getRange(ar+1,8,1,6);
  ss.insertRowsAfter(ar, 1); 
  mergeRange.mergeAcross();
}
function add2row() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var sheet = ss.getActiveSheet();
  var ar = ss.getActiveRange().getRow();
  var mergeRange = sheet.getRange(ar+1,8,2,6);
  ss.insertRowsAfter(ar, 2);
  mergeRange.mergeAcross();
}