简体   繁体   中英

Combining 3 seperate Onedit functions in google script/Sheet

I am looking to solve a problem that I have been working on last 2-3 hours which I think for many would be quite easy. I got the following function which works great although sometimes a little slow.

function onEdit(e) {
var sheet = e.source.getActiveSheet()
if (sheet.getName() !== 'Team' || e.range.getA1Notation() !== 'D16') return; 
  e.range.offset(0, 0).getValue() === 'Show' ? sheet.showRows(17, 7) : sheet.hideRows(17, 7)

I however got 2 the almost identical functions i need running at the same time. Namely

no.2

function onEdit(e) {
var sheet = e.source.getActiveSheet()
if (sheet.getName() !== 'Team' || e.range.getA1Notation() !== 'D25') return; 
  e.range.offset(0, 0).getValue() === 'Show' ? sheet.showRows(26, 10) : sheet.hideRows(26, 10)
}

No3.

function onEdit(e) {
var sheet = e.source.getActiveSheet()
if (sheet.getName() !== 'Team' || e.range.getA1Notation() !== 'D37') return; 
  e.range.offset(0, 0).getValue() === 'Show' ? sheet.showRows(38, 10) : sheet.hideRows(38, 10)
}

What I tried to get all 3 working seperately:

  1. Simply Adding all 3
  2. Adding all 3 with different function name
  3. adding multiple ifs within the same code(function) (not sure if it was done correctly though as it did not work)

you can also 'merge' the three scripts into one. Try something like this

function onEdit(e) {
var sheet, cells, rows, ind;
sheet = e.source.getActiveSheet();
cells = ['D16', 'D25', 'D37'];
rows = [[17, 7], [26, 10], [38, 10]];
ind = cells.indexOf(e.range.getA1Notation());
if (sheet.getName() !== 'Team' || ind == -1) return;
e.value === 'Show' ? sheet.showRows(rows[ind][0], rows[ind][1]) : sheet.hideRows(rows[ind][0], rows[ind][1])
}

and see if that works ?

Regarding 1 and 2

  1. Simply Adding all 3

This doesn't work because the functions use the same name. Only the last one will be ran.

  1. Adding all 3 with different function name This could work, but to be triggered by a simple on edit trigger, they need be called by function named onEdit(e). Example
function onEdit(e){
  myFunction1();
  myFunction2();
  myFunction3();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM