Google表格:添加多个列,“这些列超出范围”错误

时间:2018-12-03 10:01:38

标签: google-apps-script

嘿,我想使用脚本将多个列添加到Google表格中。我找到了this post,并按照帖子中描述的步骤进行了操作。但是,在运行脚本时,出现错误“这些列超出范围”。我只想在工作表中添加15列。该工作表还具有多个选项卡,也许这可能是问题所在?

这是脚本片段:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastCol = sheet.getLastColumn();
var maxCols = sheet.getMaxColumns();

function onOpen() { 
 // Try New Google Sheets method
 try{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Sheet Expander')
  .addItem('Add Columns', 'Add Columns')
  .addToUi(); 
 } 

// Log the error
 catch (e){Logger.log(e)}

// Use old Google Spreadsheet method
 finally{
  var items = [
  {name: 'Add Columns', functionName: 'addColumns'}
 ];
 ss.addMenu('Sheet Expander', items);
 }
}

function addColumns(){
  // Input box asks how many columns
  var numCols = Browser.inputBox("How Many Columns Would You Like to Add? (Max: " + (maxCols - lastCol) + ")");
  // That number of columns are appended to the right side of the sheet
  sheet.insertColumns(lastCol +1 , numCols);
}

0 个答案:

没有答案