简体   繁体   中英

Combining two scripts in Google sheets (Javascript)

I'm trying to combine two scripts I use on google sheets into one. They will both work on different tabs.

What is the proper way of combining scripts together?

My two scripts are as follows:

function onEdit(e) {
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'],
    cols = [1, 6, 4],
    writeCols = [15, 11],
    ind = cols.indexOf(e.range.columnStart);
if (sheets.indexOf(e.source.getActiveSheet()
    .getName()) === -1 || ind === -1 || !e.value) return;
if (ind === 0 && e.value === 'Update') {
    e.range.setValue(new Date());
} else if (ind === 1) {
    if (e.range.offset(0, 5)
        .getValue() === '') e.range.offset(0, 5)
        .setValue(2);
    if (e.range.offset(0, 9)
        .getValue() === '') e.range.offset(0, 10)
        .setValue(new Date());
   if (e.range.offset(0, -5)
        .getValue() === '') e.range.offset(0, -5)
        .setValue(new Date());
  else if (ind === 1) {
    if (e.range.offset(0, 1)
        .getValue() === 'Updated') e.range.offset(0, 1)
        .setValue(Col1);
}
}}

    function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 2 ) { //checks the column
      var nextCell = r.offset(0,12);
        nextCell.setValue(new Date());

              }
            }
  }

Any help would be much appreciated! If you can help me understand the process then I would like to learn!

Thanks so much!

How about following script? 2 script was summarized in one using "IF".

function onEdit(e) {
  var s = SpreadsheetApp.getActiveSheet();
  var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'];
  if (sheets.indexOf(e.source.getActiveSheet().getName()) > -1){
    script1();
  }
  if(s.getName() == "Must Place candis") {
    script2(s);
  }
}

function script1(){
  var cols = [1, 6, 4],
      writeCols = [15, 11],
      ind = cols.indexOf(e.range.columnStart);
  if (ind === 0 && e.value === 'Update') {
    e.range.setValue(new Date());
  } else if (ind === 1) {
    if (e.range.offset(0, 5)
        .getValue() === '') e.range.offset(0, 5)
        .setValue(2);
    if (e.range.offset(0, 9)
        .getValue() === '') e.range.offset(0, 10)
        .setValue(new Date());
    if (e.range.offset(0, -5)
        .getValue() === '') e.range.offset(0, -5)
        .setValue(new Date());
    else if (ind === 1) {
      if (e.range.offset(0, 1)
        .getValue() === 'Updated') e.range.offset(0, 1)
        .setValue(Col1);
    }
  }
}

function scritp2(s){
  if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 2 ) { //checks the column
      var nextCell = r.offset(0,12);
      nextCell.setValue(new Date());
    }
  }
}

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