Home > Mobile >  Set number of cells or rows/columns when generating Sheets in Google Sheets via script
Set number of cells or rows/columns when generating Sheets in Google Sheets via script

Time:04-06

I'm trying to generate sheets from a column in Google Sheets and it's working well. I am, however, running into an issue when too many tabs are generated. With this said, it would help if I can set up a Cell or Row/Colimn limit when genereting the new sheets.

I've tried creating a template sheet with just one cell, but it gets ignored. The code I use now is:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('Sheet1');
  const templateSheet = ss.getSheetByName('templateSheet');
  const sh_names = sh1.getRange(1,1,sh1.getLastRow(),1).getValues().flat();
  sh_names.forEach(n=>ss.insertSheet(n));
}

CodePudding user response:

You can add this to your script to keep only 100 rows and 6 columns for instance

newSheet.deleteRows(100,900)
newSheet.deleteColumns(6,20)

so

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('Sheet1');
  const sh_names = sh1.getRange(1,1,sh1.getLastRow(),1).getValues().flat();
  sh_names.forEach(function(n){
    var newSheet = ss.insertSheet(n)
    newSheet.setName(n)
    newSheet.deleteRows(100,900)
    newSheet.deleteColumns(6,20)
  });
}
  • Related