Squid1622
Squid1622

Reputation: 35

Google App Script Insert new sheet and append columns

I will start out that I am extremely new to this. I am attempting to create a script that adds a new tab to the current google sheet document but then also extends the number of columns by 26 additional columns. So far, no matter how I try it, when the new sheet is inserted, it always only adds 26 columns. I can't figure out what I'm doing wrong. Below is the most recent code I am trying to use. It is very messy (mostly because I did it using the macro builder) and I apologize for that in advance.

function AddSheet() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  //spreadsheet.getRange('D10').activate();
  spreadsheet.insertSheet(4);
  var currentCell = spreadsheet.getCurrentCell();
  var sheet = spreadsheet.getActiveSheet();
  sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
  currentCell.activateAsCurrentCell();
  spreadsheet
    .getActiveSheet()
    .insertRowsAfter(spreadsheet.getActiveSheet().getMaxRows(), 99);
}

Upvotes: 1

Views: 3623

Answers (1)

Cooper
Cooper

Reputation: 64032

Insert new sheet and append 26 columns and 100 rows

function runOne() {
  var ss = SpreadsheetApp.getActive();
  var sh=ss.insertSheet(1);//returns sheet object
  sh.insertColumnsAfter(sh.getMaxColumns(), 26);
  sh.insertRowsAfter(sh.getMaxRows(), 100);
}

You could also do it this way:

function runOne() {
  var ss = SpreadsheetApp.getActive();
  var sh=ss.insertSheet();
  sh.insertColumnsAfter(sh.getMaxColumns(), 26).insertRowsAfter(sh.getMaxRows(), 100);
}

Sheet.insertColumnsAfter(afterposition,howmany)

Spreadsheet.insertSheet

Sheet.insertRowsAfter(position, howmany)

Upvotes: 2

Related Questions