插入复制的单元格

时间:2018-02-20 17:21:49

标签: google-apps-script google-sheets

我的目标是创建一个类似于Excel的Insert Copied Cells的应用程序脚本功能。我想用鼠标输入动态选择粘贴复制范围的位置,类似于在= sum(鼠标选择)公式中选择单元格的方式。

请帮助我了解如何使用鼠标选择范围,以便将复制的行粘贴到正确的位置。

function insertCopiedCells() {
 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 
 var copyRange = spreadsheet.getActiveRange();
 var copyRangeRows = copyRange.getNumRows();

 // This is where I want to select the range with a mouse selection

 var pasteRange = spreadsheet.getRange(rangeSelection);
 var pasteRangeRow = pasteRange.getRow();
 pasteRange.activate();
 spreadsheet.insertRowsBefore(pasteRangeRow, copyRangeRows);
 copyRange.copyTo(pasteRange);
}

1 个答案:

答案 0 :(得分:0)

你需要这样的smth:

function insertCopiedCells() {
 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 
 var copyRange = spreadsheet.getActiveSheet().getActiveRange();
 var data = copyRange.getValues();

 // This is where I want to select the range with a mouse selection
 var cell = Browser.inputBox('Enter the cell address');

 var pasteCell = spreadsheet.getRange(cell)
 var h = data.length;
 var l =  data[0].length;

 spreadsheet.insertRowsBefore(pasteCell.getRow(), h)
 var pasteRange = pasteCell.offset(0, 0, h, l);
 pasteRange.setValues(data);
}

从自定义菜单中启动它:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Menu')
      .addItem('Copy Selected', 'insertCopiedCells')
      .addToUi();
}