I have created a form in Google Forms. As part of the form, there is a picture taken and that is stored on my Google Drive. The data populates a Google sheet. A column has been added to the beginning (column A) of the Form Responses tab which has a checkbox. A second tab has been created that creates a printable form of the submitted data that has the checkbox selected.
I am trying to write a script that, when a new submission comes in, it automatically clears all the checkboxes, selects the checkbox in the last row where there is data in column B. The script will then create a .pdf of the form tab and attach it to an email to me. In addition, the photo that was taken (url in column AX of Form Response Tab) should also be extracted from Google drive and the image file also attached to the email.
function clearCheckboxesAndSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSheet = ss.getSheetByName("Form Responses 1");
var jhaSheet = ss.getSheetByName("Form Tab");
// Clear all checkboxes in column A of "Form Responses 1" sheet
var checkboxRange = formSheet.getRange("A:A");
var checkboxes = checkboxRange.getValues();
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i][0] === true) {
checkboxRange.getCell(i + 1, 1).uncheck();
}
}
// Find the last row with data in column B
var lastRowWithData = formSheet.getLastRow();
while (formSheet.getRange(lastRowWithData, 2).isBlank() && lastRowWithData > 1) {
lastRowWithData--;
}
// Place a check in the checkbox of the last row where column B has data
formSheet.getRange(lastRowWithData, 1).check();
// Create PDF of "Form Tab" sheet
var fileId = ss.getId();
var blob = DriveApp.getFileById(fileId).getAs('application/pdf').setName('Job_Hazard_Analysis.pdf');
// Create email
var emailAddress = "My Email";
var subject = "Form Report";
var body = "Please find the attached Form report.";
// Send email with attached PDF
MailApp.sendEmail({
to: emailAddress,
subject: subject,
body: body,
attachments: [blob]
});
}
So when I run this script, it creates the .pdf out of all tabs of the spreadsheet, not just the Forms tab. Also, the picture that was taken is not attached. Instead, it sends (2) pictures from the drive that I have no idea why it chooses them. It's always the same two images.
Any help would be appreciated.