简体   繁体   中英

Google App Script - pass multiple sheet ID through function

Very new to coding. The below code works but is slow - hoping you can assist me to speed it up.

I have one Google sheet, with a tab "Data", this I use to populate my all my other Google form dropdown menus.

However I'm setting up more and more forms that require this info, so speeding this up would be great. (Being able to set this code to run on Form 'Open' would be the next step)

'''

function two() {
  main();
  main2();
  main3();
}






var ssID = "1cx5I5_JABvS5bi0R7fM9tiRp_XXa28EvuCQm0I7lNSI";
var formID = "1jIhQ7rdJ2u_BKR9Fsj_-k9eX1ngUvRtyigFlSNqrMT0";  // Old Ordering system https://docs.google.com/forms/d/1jIhQ7rdJ2u_BKR9Fsj_-k9eX1ngUvRtyigFlSNqrMT0/edit
var formID2 = "1GsqBYMCKQKFpg0qmfZmRDdC6TNR6Q-sWzwokeNbdm44";  // Labour broking https://docs.google.com/forms/d/1GsqBYMCKQKFpg0qmfZmRDdC6TNR6Q-sWzwokeNbdm44/edit
var formID3 = "1XvGLV0PzOYfG9ryvNKxI9Nwi62NjeZ4BCFlBNG1PS0Y"; //Site planing https://docs.google.com/forms/d/1XvGLV0PzOYfG9ryvNKxI9Nwi62NjeZ4BCFlBNG1PS0Y/edit

var wsData = SpreadsheetApp.openById(ssID).getSheetByName("data");
var form = FormApp.openById(formID);
var form2 = FormApp.openById(formID2);
var form3 = FormApp.openById(formID3);

//BELOW IS HOW WE STARTED WORKING OUT HOW TO GET THE INFO WE NEEDED
//function myFunction() {
  
  //var item = form.getItemById(831533065);
  //var values = ["Nic","Tobie"];
  //item.asListItem().setChoiceValues(values);
  //var items = form.getItems();
  //Logger.log(items[0].getId().toString());

  
//}






function main(){
  var labels = wsData.getRange(1, 1,1,wsData.getLastColumn()).getValues()[0];
  
  
  labels.forEach(function(label,i){
    //Logger.log(label);
    var options = wsData
                   .getRange(2, i + 1,wsData.getLastRow()-1,1)
                   .getValues()
                   .map(function(o){ return o[0] })
                   .filter(function(o){ return o !== ""});
    //Logger.log(options);
    
    
    updateDropdownUsingTitle(label,options);
  });
}


function updateDropdownUsingTitle(title,values) {
  //var title = "Name of person completing this form";
  //var values = ["r","f","p"];
  
  var items = form.getItems();
  var titles = items.map(function(item){
    return item.getTitle()
  });
  
  var pos = titles.indexOf(title);
  if(pos !== -1){  
    var item = items[pos];
    var itemID = item.getId(); 
    updteDropdown(itemID,values);
  }
 
}


function updteDropdown(id,values) {
  
  var item = form.getItemById(id);
  item.asListItem().setChoiceValues(values);
  
}




function main2(){
  var labels2 = wsData.getRange(1, 1,1,wsData.getLastColumn()).getValues()[0];
  
  
  labels2.forEach(function(label2,i){
    //Logger.log(label);
    var options2 = wsData
                   .getRange(2, i + 1,wsData.getLastRow()-1,1)
                   .getValues()
                   .map(function(o){ return o[0] })
                   .filter(function(o){ return o !== ""});
    //Logger.log(options);
    
    
    updateDropdownUsingTitle2(label2,options2);
  });
}


function updateDropdownUsingTitle2(title2,values2) {
  //var title = "Name of person completing this form";
  //var values = ["r","f","p"];
  
  var items2 = form2.getItems();
  var titles2 = items2.map(function(item){
    return item.getTitle()
  });
  
  var pos = titles2.indexOf(title2);
  if(pos !== -1){  
    var item2 = items2[pos];
    var itemID2 = item2.getId(); 
    updteDropdown2(itemID2,values2);
  }
 
}


function updteDropdown2(id,values2) {
  
  var item2 = form2.getItemById(id);
  item2.asListItem().setChoiceValues(values2);
  
}



function main3(){
  var labels3 = wsData.getRange(1, 1,1,wsData.getLastColumn()).getValues()[0];
  
  
  labels3.forEach(function(label3,i){
    //Logger.log(label);
    var options3 = wsData
                   .getRange(2, i + 1,wsData.getLastRow()-1,1)
                   .getValues()
                   .map(function(o){ return o[0] })
                   .filter(function(o){ return o !== ""});
    //Logger.log(options);
    
    
    updateDropdownUsingTitle3(label3,options3);
  });
}


function updateDropdownUsingTitle3(title3,values3) {
  //var title = "Name of person completing this form";
  //var values = ["r","f","p"];
  
  var items3 = form3.getItems();
  var titles3 = items3.map(function(item){
    return item.getTitle()
  });
  
  var pos = titles3.indexOf(title3);
  if(pos !== -1){  
    var item3 = items3[pos];
    var itemID3 = item3.getId(); 
    updteDropdown3(itemID3,values3);
  }
 
}


function updteDropdown3(id,values3) {
  
  var item3 = form3.getItemById(id);
  item3.asListItem().setChoiceValues(values3);
  
}

'''

Since you populate all forms with the same data, you can optimize your code by retrieving the data only once and removing all repetitive functions and requests

Important:

  • Avoid performing calls to SpreadsheetApp more often than necessary, since it will make your code slow - see Best Practices

Sample:

var ssID = "1cx5I5_JABvS5bi0R7fM9tiRp_XXa28EvuCQm0I7lNSI";
var formID = "1jIhQ7rdJ2u_BKR9Fsj_-k9eX1ngUvRtyigFlSNqrMT0";  // Old Ordering system https://docs.google.com/forms/d/1jIhQ7rdJ2u_BKR9Fsj_-k9eX1ngUvRtyigFlSNqrMT0/edit
var formID2 = "1GsqBYMCKQKFpg0qmfZmRDdC6TNR6Q-sWzwokeNbdm44";  // Labour broking https://docs.google.com/forms/d/1GsqBYMCKQKFpg0qmfZmRDdC6TNR6Q-sWzwokeNbdm44/edit
var formID3 = "1XvGLV0PzOYfG9ryvNKxI9Nwi62NjeZ4BCFlBNG1PS0Y"; //Site planing https://docs.google.com/forms/d/1XvGLV0PzOYfG9ryvNKxI9Nwi62NjeZ4BCFlBNG1PS0Y/edit
var wsData = SpreadsheetApp.openById(ssID).getSheetByName("data");
var form1 = FormApp.openById(formID);
var form2 = FormApp.openById(formID2);
var form3 = FormApp.openById(formID3);
var formArray = [form1, form2, form3];

function main(){
  var labels = wsData.getRange(1, 1,1,wsData.getLastColumn()).getValues()[0];
  var values = wsData.getRange(2, 1, wsData.getLastRow()-1,wsData.getLastColumn()).getValues();
  formArray.forEach(function(form){
    var items = form.getItems();
    var titles = items.map(function(item){
      return item.getTitle()
    });
    labels.forEach(function(label,i){
      //Logger.log(label);
      var options = values.map(function(row){return row[i]})
      .filter(function(o){ return o !== ""});
      //Logger.log(options);    
      var pos = titles.indexOf(label);
      if(pos !== -1){  
        var item = items[pos];
        var itemID = item.getId(); 
        item.asListItem().setChoiceValues(options);
      }
    });
  })
}

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