Home > Enterprise >  Google script function works when it runs separately, but when I run all the functions together it d
Google script function works when it runs separately, but when I run all the functions together it d

Time:09-08

I've got a question about my script. I've got a script where when running together only 2 out of 3 functions run, the other function does not run. The one function that does not work when running together is the "medewerkerLijst" function. When I run that function on it's own it works perfectly as it should, when running it together with the other 2 functions it does not work. I can't figure out why it's not working.

The functions "nieuwDossier" & "sorteerDossiers" do work when running together. Sorry if my code is a mess, I'm still learning how to code.

function Aanmaken(){
  medewerkerLijst();
  nieuwDossier();
  sorteerDossiers();

}

function medewerkerLijst() {
  var ss5 = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet2 = ss5.getSheetByName("-Dossier");
  var pasteSheet2 = ss5.getSheetByName("-Medewerkers");

  // get source range
  var source2 = copySheet2.getRange(2,7,1,1);
  // get destination range
  var destination2 = pasteSheet2.getRange(pasteSheet.getLastRow() 1,1,1,1);

  // copy values to destination range
  source2.copyTo(destination2, {contentsOnly:true});
}

function nieuwDossier() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = source.getSheetByName('-Template');

  sheet.copyTo(source).setName('Nieuw');
  var sheet2 = source.getSheetByName('-Dossier');
  var cell = sheet2.getRange("G2");
  var value = cell.getValue();
  var sheet3 = source.getSheetByName('Nieuw');
  sheet3.setName(value);
  sheet3.setTabColor(null);
}

function sorteerDossiers () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetNameArray = [];
  var sheets = ss.getSheets();
   
  for (var i = 0; i < sheets.length; i  ) {
    sheetNameArray.push(sheets[i].getName());
  }
  
  sheetNameArray.sort();
    
  for( var j = 0; j < sheets.length; j   ) {
    ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
    ss.moveActiveSheet(j   1);
  }
}

CodePudding user response:

This runs just fine

function main() {
  var ss = SpreadsheetApp.getActive();
  var sh1 = ss.getSheetByName("Sheet1");
  var sh2 = ss.getSheetByName("Sheet2");
  var rg1 = sh1.getRange(2, 7, 1, 1);
  var rg2 = sh2.getRange(sh2.getLastRow()   1, 1, 1, 1);
  rg1.copyTo(rg2, { contentsOnly: true });
  var sh3 = ss.getSheetByName('Sheet3');
  var sh4 = sh3.copyTo(ss).setName('Sheet4');
  var cell = sh1.getRange("G2");
  var v1G2 = cell.getValue();
  sh4.setName(v1G2);
  sh4.setTabColor(null);
  var shts = ss.getSheets().map(sh => sh.getName()).sort().forEach((n, i) => {
    ss.setActiveSheet(ss.getSheetByName(n));
    ss.moveActiveSheet(i   1)
  });
}
  • Related