skip to Main Content

Please help me with the script I want to run when I check a specific checkbox in Google Sheets. It works for me when I put it with a button but it doesn’t work on tablets so I want to make it with a checkbox.

function sendExportedSheetAsPDFAttachment() {
let blob =     getFileAsBlob("https://docs.google.com/spreadsheets/d/1FuTHRBQ0JRmo35kcrFBIhzjRXGTea3Xznek1lKE6xT0/export?format=pdf&scale=3");
blob.setName("ΘΕΛΩ ΕΧΩ ΕΘ.ΑΝΤΙΣΤΑΣΕΩΣ");
  var message = {
    to: "[email protected]",
    subject: "ΘΕΛΩ ΕΧΩ ΕΘ.ΑΝΤΙΣΤΑΣΕΩΣ",
    body: "Χαίρεται,nnΠαρακαλώ βρείτε συννημένη τη σημερινή     παραγγελία.nnΕυχαριστώ,nΠαραγγελίες",
        name: "ΕΘ.ΑΝΤΙΣΤΑΣΕΩΣ",
    attachments: [blob.setName("ΘΕΛΩ ΕΧΩ ΕΘ.ΑΝΤΙΣΤΑΣΕΩΣ")]
  }

  MailApp.sendEmail(message);
}

and the other is related to the script above

function getFileAsBlob(exportUrl) {
 let response = UrlFetchApp.fetch(exportUrl, {
     muteHttpExceptions: true,
     headers: {
       Authorization: 'Bearer ' +  ScriptApp.getOAuthToken(),
     },
   });
 return response.getBlob();
}

Thank you very much.

It works for me when I put it with a button but it doesn’t work on tablets so I want to make it with a checkbox.

2

Answers


  1. Running a Script When Checkbox Is Ticked

    First Method

    There are two ways to run a script when a checkbox is ticked. First, you can use the google sheets built-in if condition. To do this, paste the following into any empty cell. Make sure to replace A1 with the location of your checkbox.

    =IF(A1,sendExportedSheetAsPDFAttachment(),"")
    

    This will run your function when the checkbox is ticked, and nothing when the checkbox is unticked.

    Second Method

    Paste the following code into the Google Apps Script where you have your functions, found on Extensions > Apps Script

    function onEdit(event) {
      var range = event.range;
      if (range.getA1Notation() == 'A1') {
        var value = range.getValue();
        if (typeof value === 'boolean' && value == true) {
          range.setNote("Box ticked on " + new Date())
          sendExportedSheetAsPDFAttachment();
        }
      }
    }
    

    Once again, replace A1 with the location of your checkbox.

    Login or Signup to reply.
  2. Make it an installable trigger

    function onMyEdit(e) {//make it an installable trigger
      const sh = e.range.getSheet();
      if (sh.getName() == "Your sheet name" && e.range.columnStart == 1 && e.range.rowStart == 1 && e.value == "TRUE") {
        sendExportedSheetAsPDFAttachment();
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search