Home > Software engineering >  Getting Multiple Functions to Run Together Successfully
Getting Multiple Functions to Run Together Successfully

Time:09-13

I'm new to Google Apps Script and coding in general. I have a pretty specific goal I am trying to accomplish: that is to have a pop-up generated when a checkbox is selected, displaying a range of information from another sheet. I have written a script to help me accomplish this. The myFunction part of the script works if I use it alone with the built-in Google trigger for any cell edits. However, I'm trying to make it so it runs automatically when a box is checked specifically from one column (column C, starting with Row 4) with the onEdit function. It's taken me a long time to even make it this far, what am I missing to make it run properly? Any help is appreciated!

function Player_Detail(){
  onEdit(e);
  myfunction();
}

function onEdit(e) { 
    
  const row = e.range.getRow();
  const col = e.range.getColumn();
  const as = e.source.getActiveSheet();
  
  if(as.getName() == 'Top Projected Prospects'&& row>3 && col==3);
  }

function myfunction(){
  const ui = SpreadsheetApp.getUi();
  const html = HtmlService.createHtmlOutput('<iframe width="670" height="685" src="https://docs.google.com/spreadsheets/d/1zdHfv8nMQSQ1WsnZKdCtvs7BXJZftF95Ml6f3rhRYmo/edit#gid=1458157110"></iframe>');
  html.setWidth(710);
  html.setHeight(710);

  var spreadsheet = SpreadsheetApp.getActive();

  spreadsheet.getCurrentCell().setValue('FALSE');

  var range_to_copy = spreadsheet.getCurrentCell().offset(0, -2);
  var range_to_paste = spreadsheet.getSheetByName('Player Detail').getRange(2, 3, 1, 1);

  range_to_copy.copyTo(range_to_paste, {contentsOnly: true});

  ui.showModalDialog(html, "Player Detail");

  //  ui.showModelessDialog(html, "Player Detail");
  //  ui.showModalDialog(html, "Player Detail");
}

Okay, I've made some edits. Now the script appears to behave as expected, when I check a box (in column C, beginning row 4) it copies my ID and pastes it to the other sheet as it's supposed to. However now the modal dialog box is not appearing with that sheet's range of info. After testing, this appears to be the only thing that's not working and once I get it ironed out, my script should be complete! Here is the current script:

 function onEdit(e) { 
        
      const range = e.range;
        if (range.getRow() > 3 &&
        range.getColumn() === 3) {
    
      var spreadsheet = SpreadsheetApp.getActive();
    
      spreadsheet.getCurrentCell().setValue('FALSE');
    
      var range_to_copy = spreadsheet.getCurrentCell().offset(0, -2);
      var range_to_paste = spreadsheet.getSheetByName('Player Detail').getRange(2, 3, 1, 1);
    
      range_to_copy.copyTo(range_to_paste, {contentsOnly: true});
    
      const ui = SpreadsheetApp.getUi();
      const html = HtmlService.createHtmlOutput('<iframe width="670" height="685" src="https://docs.google.com/spreadsheets/d/1zdHfv8nMQSQ1WsnZKdCtvs7BXJZftF95Ml6f3rhRYmo/edit#gid=1458157110"></iframe>');
      html.setWidth(710);
      html.setHeight(710);
    
      ui.showModalDialog(html, "Player Detail");
    
      //  ui.showModelessDialog(html, "Player Detail");
      //  ui.showModalDialog(html, "Player Detail");
      }
    }

CodePudding user response:

this is basically the same thing on less function calls although may be you don't care since you have a dialog launched in there. Did you know that functions called by simple triggers must finish within 30 seconds?

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "Your Sheet Name" && e.range.rowStart > 3 && e.range.columnStart == 3) {
    e.range.setValue('FALSE');
    var range_to_copy = e.range.offset(0, -2);
    var range_to_paste = e.source.getSheetByName('Player Detail').getRange(2, 3);
    range_to_copy.copyTo(range_to_paste, { contentsOnly: true });
    const ui = SpreadsheetApp.getUi();
    const html = HtmlService.createHtmlOutput('<iframe width="670" height="685" src="https://docs.google.com/spreadsheets/d/1zdHfv8nMQSQ1WsnZKdCtvs7BXJZftF95Ml6f3rhRYmo/edit#gid=1458157110"></iframe>');
    html.setWidth(710);
    html.setHeight(710);
    ui.showModalDialog(html, "Player Detail");
  }
}
  • Related