1

I have two buttons (Button1 and Button2) and one function: MyFunction(number). And I either need to pass a parameter to the function or find out what button the function was started from. Is it possible?

function MakePDF(number) {

  var ui = SpreadsheetApp.getUi();

  //Get Active Spreadsheet
  var spreadSheet=SpreadsheetApp.getActiveSpreadsheet();
  spreadSheet.getRange('B2').setValue(number); //HERE I NEED TO GET THE SPECIFIC NUMBER FROM 1 TO 100

  //Get Sheet to print of the spreadsheets
  var sheets=spreadSheet.getSheets();
  var Faktura = spreadSheet.getSheetByName("Invoice");
  var sheetID = Faktura.getSheetId();

  //Export URL with Parameters
  var spreadSheetId = spreadSheet.getId();
  var URL = "https://docs.google.com/spreadsheets/d/"+spreadSheetId+"/export"+
                                                        "?format=pdf&"+
                                                        "size=7&"+
                                                        "fzr=false&"+
                                                        "portrait=true&"+
                                                        "fitw=true&"+
                                                        "gridlines=false&"+
                                                        "printtitle=false&"+
                                                        "sheetnames=false&"+
                                                        "pagenum=UNDEFINED&"+
                                                        "attachment=true&"+
                                                        "gid="+sheetID; 
   //the HTTP method for the request: get and headers : authorization : Bearer tokens to access OAuth 2.0-protected resources
   var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

   //Return the data inside this object as a blob.
   var response=UrlFetchApp.fetch(URL,params).getBlob();

   //To set name of file
   var VS = listOvladani.getRange('B6').getValue();
   var firma = listOvladani.getRange('B5').getValue();
   firma = removeDiak(firma);
   firma = firma.toString().replace(/ /g, '-');
   firma = firma.toString().replace(/\./g,'');
   firma = firma.toString().replace(/,/g,'');

   var namePDF = VS + "_" + firma + "_Autonapul.pdf";

   // Load it to specific directory
   var dir = DriveApp.getFoldersByName("Rucnifaktury").next();
   var pdfFile = dir.createFile(response).setName(namePDF);

   // Display a modal dialog box with custom HtmlService content.
   const htmlOutput = HtmlService
        .createHtmlOutput('<p>Click to open <a href="' + pdfFile.getUrl() + '" target="_blank">' + spreadSheet.getName() + '</a></p>')
        .setWidth(300)
        .setHeight(80)
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Export Successful');


   //Email it
/*   MailApp.sendEmail('trnka@trnka.cz', 'Pokus', 'Nějaký text', {
        attachments: [{
            fileName: "Faktura_pokusna" + ".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });
*/    

}

More details More details More details More details More details More details More details More details More details More details More details More details

2 Answers2

2

You can do it in a different way:

Replace buttons through checkboxes and bind to your script an onEdit(e) trigger which will automatically fire the script on each edit.

You can implement a statement to verify either the edited column was your checkbox column and if so - which checkbox has been checked.

Sample:

enter image description here

function onEdit(e) {
  if(e.range.getColumn()==2&&e.range.getValue()==true){
  Logger.log(e.range.getA1Notation());
  }
}

References:

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thank you, but I tried onEdit but it stoped on this row of script: var response=UrlFetchApp.fetch(URL,params).getBlob(); - without error alert, only it do nothing from this place. I tried this code: https://yagisanatode.com/2019/02/13/google-apps-script-when-i-add-a-value-to-a-cell-in-a-selected-column-i-want-it-to-move-to-another-google-sheet/ everything worked, but the export failed – Ludvík Trnka Sep 26 '19 at 14:20
  • I see. You cannot call UrlFetchApp on simple trigger: https://stackoverflow.com/a/31112992/11599789 In this case, just replace the simple onEdit() trigger through an installable one: https://developers.google.com/apps-script/guides/triggers/installable – ziganotschka Sep 26 '19 at 14:26
  • So ok, thank you - I will try it once more. And to the original question - is there any way to know which button has been pressed? Without onEdit? – Ludvík Trnka Sep 26 '19 at 14:42
  • If your button is a drawing to which you attached a script - unfortunately the answer is "No". – ziganotschka Sep 26 '19 at 14:54
  • 1
    So I used your solution with function SpecialOnEdit() for which I created my trigger. And it works great. It's easier than I thought. Still others with the ability to edit must try how it works. – Ludvík Trnka Sep 28 '19 at 12:07
2

You can use two more functions who just call the main function with a different parameter, so button 1 calls pressButton1 and button 2 calls pressButton2.

function MakePDF(number) {
  //do stuff
}
function pressButton1(){
  MakePDF(1);
}
function pressButton2(){
  MakePDF(2);
}

This is the easiest way to handle the situation.

Hink
  • 2,271
  • 1
  • 11
  • 8