How to run a script on multiple sheets, Google Sheets

倖福魔咒の 提交于 2021-01-29 06:11:58

问题


I have a script that I would like to run on specific tabs in my Google worksheet, but not necessarily all of the tabs. I tried doing two differently named scripts, but only the last one that was saved will run. How can I code this script to run on specific tabs? This is my beginning script:

function onEdit() {
  var s = SpreadsheetApp.getActive().getSheetByName('Proposal');
  s.showRows(1, s.getMaxRows());

  s.getRange('B:B')
    .getValues()
    .forEach( function (r, i) {
      if (r[0] == 'Hide') 
        s.hideRows(i + 1);
    });
}

I tried modifying like this, using suggestions from another question posted, but it did not work.

function onEdit() {
  var tabs = [
        'Proposal',
        'Materials List - All',
        'Materials List - Shingles',
        'Materials List - Access',
        'Work Order'
  ];

  var ss=SpreadsheetApp.getActiveSpreadsheet();
  for (var i = 0; i < tabs.length; i++) {
    var sheet = ss.getSheetByName(tabs[i]);
    s.showRows(1, s.getMaxRows());

    s.getRange('B:B')
      .getValues()
      .forEach( function (r, i) {
        if (r[0] == 'Hide') 
          s.hideRows(i + 1);
      });
  }

Any suggestions on how to do this properly? How could I do this on all the tabs if I decided that was easier?

Thank you!


回答1:


You can do it 2 ways. Using 'exclude' or 'only'.

exclude means, do not do it if current sheet is one of these.

only means, do it only if current sheets is one of these.

Use below code and enable any one option then try. To enable, add respective sheet names to excludes or only array and uncomment lines by taking out //. It'll work only on current sheet, not all sheets in the spreadsheets.

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();

  // use only one of these 2 options

  // add sheets names to exclude, example ['Sheet1', 'Sheet2']
  // to exclude these sheets enable these 2 lines below by uncommenting
  // var excludes = [];
  // if (excludes.indexOf(s.getName()) != -1) return;

  // add sheet names to work on only these sheets, as above
  // to work on only these sheets enable these 2 lines below by uncommenting
  // var only = [];
  // if (only.indexOf(s.getName()) == -1) return;

  s.showRows(1, s.getMaxRows());
  s.getRange('B:B')
    .getValues()
    .forEach(function(r, i) {
      if (r[0] == 'Hide') s.hideRows(i + 1);
    });
}

EDIT

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var only = [
    'Proposal',
    'Materials List - All',
    'Materials List - Shingles',
    'Materials List - Access',
    'Work Order'
  ];
  only.forEach(function(name) {
    var s = ss.getSheetByName(name);
    s.showRows(1, s.getMaxRows());
    s.getRange('B:B')
      .getValues()
      .forEach(function(r, i) {
        if (r[0] == 'Hide') s.hideRows(i + 1);
      });
  });
}


来源:https://stackoverflow.com/questions/55224103/how-to-run-a-script-on-multiple-sheets-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!