Hello
I have an app where you can book a ticket for an event. The app has about 20-30 events per year. For each event you can register and then receive an SMS confirmation. Each event has its own sheet. With App Script a function is created per sheet. A trigger that checks if there is a new entry triggers the following script and sends an SMS via Twilio:
var TWILIO_ACCOUNT_SID = 'XXXX';
var TWILIO_SMS_NUMBER = 'XXXX';
var TWILIO_AUTH_TOKEN = 'XXXX';
function Test_SMS() {
var ss = SpreadsheetApp.getActive().getSheetByName("Sheet-1");
var values = ss.getRange("A2:K").getValues();
for (var i in values) {
var row=i;
var r = +row + 2;
if (values[i][0] != '' && values[i][1] == '' ) {
var number = '+43'+values[i][0];
var vorname = values[i][2];
var ticket = values[i][6];
var datum = values[i][10];
function sendSms(to, body) {
var messages_url = 'https://api.twilio.com/2010-04-01/Accounts/' + TWILIO_ACCOUNT_SID + '/Messages.json';
var payload = {
"To": to,
"Body" : 'Bestätigung: \nHallo ' + vorname + ', du hast dich angemeldet für am ' + datum + '.\n\nDeine Ticketart ist: ' + ticket + '. ',
"From" : 'XXXX'
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(TWILIO_ACCOUNT_SID + ":" + TWILIO_AUTH_TOKEN)
};
UrlFetchApp.fetch(messages_url, options);
}
try {
response_data = sendSms(number, vorname, ticket, datum);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error";
}
ss.getRange(r, 2).setValue(status);
}
}
}
The problem is that for each event I have to adjust the function again and that generates a lot of code. Now I wanted to ask if it is possible to change this function so that I can call several sheets at once, so that I don’t have to customize the script individually per sheet?
I have already started an attempt here, but it only sends an SMS in Sheet-1 and also reports the following error:
TypeError: Cannot call method “getRange” from null.
var TWILIO_ACCOUNT_SID = 'XXXX';
var TWILIO_SMS_NUMBER = 'XXXX';
var TWILIO_AUTH_TOKEN = 'XXXX';
var sheetnames = ['Sheet-1', 'Sheet-2'];
sheetnames.forEach(function (name) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(name);
var range = sheet.getRange('A2:K');
var values = range.getValues();
Test_SMS(sheet, values);
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function Test_SMS(sheet, values) {
for (var i in values) {
var row=i;
var r = +row + 2;
if (values[i][0] != '' && values[i][1] == '' ) {
var number = '+43'+values[i][0];
var vorname = values[i][2];
var ticket = values[i][6];
var datum = values[i][10];
function sendSms(to, body) {
var messages_url = 'https://api.twilio.com/2010-04-01/Accounts/' + TWILIO_ACCOUNT_SID + '/Messages.json';
var payload = {
"To": to,
"Body" : 'Bestätigung: \nHallo ' + vorname + ', du hast dich angemeldet für am ' + datum + '.\n\nDeine Ticketart ist: ' + ticket + '. ',
"From" : 'XXXX'
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(TWILIO_ACCOUNT_SID + ":" + TWILIO_AUTH_TOKEN)
};
UrlFetchApp.fetch(messages_url, options);
}
try {
response_data = sendSms(number, vorname, ticket, datum);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error";
}
sheet.getRange(r, 2).setValue(status);
}
}
}
I am very grateful for any help.
Many thanks
(Excuse me for my bad english)