vendredi 22 novembre 2019

Can You Use a For Loop(s) to Optimize This Code?

I am hoping there is a cleaner way to write this code. What I have below works exactly as intended, however, I will have to do this for multiple different sections, so before I do that I am looking to see if there is a way potentially using for loops or something that would shorten this code and help it to run faster.

The user can enter a number of images from as few as 1 to as many as 4. Depending on the number they select, a new line is created for each image to be placed. This therefore pushes lines at the bottom of the spreadsheet that many lines lower, so this needs to be accounted for to ensure the information still gets inserted in the correct location regardless of how many images the user chooses. This is written in Google Apps Scripts within Google Sheets. If the value of the variables below is anything greater than 2, the user wants an image there, less than 3 they do not want an image and no new line will be created.

  var graphic1 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic1_loc).getValue();
  var graphic2 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic2_loc).getValue();
  var graphic3 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic3_loc).getValue();
  var graphic4 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic4_loc).getValue();
  var graphic5 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic5_loc).getValue();

  var website_placement = 'B47';
  var website_placement2 = 'B48';
  var website_placement3 = 'B49';
  var website_placement4 = 'B50';
  var website_placement5 = 'B51';

      if (graphic2.length < 3 && graphic3.length < 3 && graphic4.length < 3 && graphic5.length < 3) {
        var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
        SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement).setValue('For the latest forecast updates, visit ' + website+'.');
      }
      else if (((graphic2.length > 2) && (graphic3.length < 3) && (graphic4.length < 3) && (graphic5.length < 3)) ||
               +((graphic2.length < 3) && (graphic3.length > 2) && (graphic4.length < 3) && (graphic5.length < 3)) ||
               +((graphic2.length < 3) && (graphic3.length < 3) && (graphic4.length > 2) && (graphic5.length < 3)) ||
               +((graphic2.length < 3) && (graphic3.length < 3) && (graphic4.length < 3) && (graphic5.length > 2))) {
        var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
        SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement2).setValue('For the latest forecast updates, visit ' + website+'.');
      }
      else if ((graphic2.length > 2 && graphic3.length > 2 && graphic4.length < 3 && graphic5.length < 3) ||
               +(graphic2.length > 2 && graphic3.length < 3 && graphic4.length > 2 && graphic5.length < 3) ||
               +(graphic2.length > 2 && graphic3.length < 3 && graphic4.length < 3 && graphic5.length > 2) ||
               +(graphic2.length < 3 && graphic3.length > 2 && graphic4.length > 2 && graphic5.length < 3) ||
               +(graphic2.length < 3 && graphic3.length > 2 && graphic4.length < 3 && graphic5.length > 2) ||
               +(graphic2.length < 3 && graphic3.length < 3 && graphic4.length > 2 && graphic5.length > 2)) {
        var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
        SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement3).setValue('For the latest forecast updates, visit ' + website+'.');
      }
      else if (((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length < 3)) || 
               +((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length < 3) && (graphic5.length > 2)) ||
               +((graphic2.length > 2) && (graphic3.length < 3) && (graphic4.length > 2) && (graphic5.length > 2)) ||
               +((graphic2.length < 3) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length > 2))) {
        var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
        SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement4).setValue('For the latest forecast updates, visit ' + website+'.');
      }
      else if ((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length > 2)) {
        var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
        SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement5).setValue('For the latest forecast updates, visit ' + website+'.');
      }

Aucun commentaire:

Enregistrer un commentaire