paint-brush
How to Use App Scripts to Automate in Google Sheetsby@riddhesh
236 reads

How to Use App Scripts to Automate in Google Sheets

by Riddhesh
Riddhesh HackerNoon profile picture

Riddhesh

@riddhesh

Fullstack Developer and founder of Code B https://code-b.dev/

January 19th, 2024
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A requirement to send a text message in the form of an SMS message to a patient when there is an entry for an appointment scheduled for them and logged into the Google Sheets. Create a custom function that can be triggered by clicking on the button. Run and give permissions.
featured image - How to Use App Scripts to Automate in Google Sheets
1x
Read by Dr. One voice-avatar

Listen to this story

Riddhesh HackerNoon profile picture
Riddhesh

Riddhesh

@riddhesh

Fullstack Developer and founder of Code B https://code-b.dev/

About @riddhesh
LEARN MORE ABOUT @RIDDHESH'S
EXPERTISE AND PLACE ON THE INTERNET.

Our Problem Statement:

A requirement to send a text message in the form of an SMS message to a patient when there is an entry for an appointment scheduled for them and logged into the Google Sheets.


Sample data:

image


Step 1: Add a button in the Google sheet


Insert -> Drawing

image


Step 2: Create a custom function that can be triggered by clicking on the button.


Extensions -> Apps Script

image


image


Step 3: Code to send SMS for appointments


function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('appoinments');
ja
// 2 dimentional array for sheets data
var data = sheet.getDataRange().getValues();
var timeZone = Session.getScriptTimeZone()


for (var i = 1; i < data.length; i++) {

// condition to send message to whome we have not already sent message and first column is not blank
if (data[i][4] != "done" && data[i][0] != "") {
var date = Utilities.formatDate(data[i][2], timeZone, 'dd-MM-yyyy')
var time = Utilities.formatDate(data[i][3], timeZone, 'h:mm a')
var name = data[i][0]
var phonenumber = data[i][1]

Logger.log("date: "+date)
Logger.log("time: "+time)
Logger.log("name: "+ name)
Logger.log("phonenumber: "+phonenumber)

// API to send SMS(Any provider)
// Hit api to send sms
// var url = `Put your sms provider url here`

// var response = UrlFetchApp.fetch(url);
// var json = response.getContentText();

// set status to done, so we dont send messages again for same appoinments
sheet.getRange(i + 1, 5).setValue('done')
}
}
}



Step 4: Run and give permissions.

image


Click on Advance on the bottom left:

image


Click on, “Go to the untitled project.”

image


Click on allow:

image


We can see logs as expected:

image


Now, we can see the status column set to “done.”

image


Step: Attach the function to the button.


Click on the button > assign a script > write the name of the function that we just made.

image


image

Now, add new appointments to the sheet, and keep the appointment status blank. When we click on the send message button, an SMS will be sent, and the status will be updated in the sheet.


Share this with anybody you think would benefit from this. Have any suggestions or questions? Feel free to message me on LinkedIn.


Originally Written by Riddhesh Ganatra on Medium.com

L O A D I N G
. . . comments & more!

About Author

Riddhesh HackerNoon profile picture
Riddhesh@riddhesh
Fullstack Developer and founder of Code B https://code-b.dev/

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Tefter
Coffee-web
Thetechstreetnow
Devurls
X REMOVE AD