Google Sheets: send SMS from a sheet
An Apps Script sends each new row through the SendHarbor API and writes the message status back into the same row.
What you need
- A Google account with access to Google Sheets.
- A SendHarbor API key with the rights messages:send and messages:read. A sandbox key is enough for the first test.
- For real SMS, the phone ID of your connected Android phone.
No account or key yet? The quick start creates an account, connects your Android phone and shows ready code for this tool with your key. Quick start on your phone
How it works
The script writes a unique key into the row before it sends. If the request is repeated with this key, SendHarbor returns the same message and sends no second SMS. A timer reads the status every five minutes until the message is delivered, failed or left without a delivery report.
Steps
-
Prepare the sheet
Create a sheet named SMS. Put these headings into the first row: Phone, Text, Key, Message ID, Status, Checked at.
Format the Phone column as plain text (Format → Number → Plain text), so the leading plus is kept. Write numbers in international format, for example +15551234567.
Check: the first row holds six headings, and a number you type keeps its plus.
-
Add the script
Open Extensions → Apps Script, replace the contents of Code.gs with the code below and save.
Code.gs // SendHarbor recipe for Google Sheets (Apps Script, V8 runtime). // Sheet "SMS": A Phone, B Text, C Key, D Message ID, E Status, F Checked at. const SENDHARBOR_URL = 'https://sendharbor.app'; const SHEET_NAME = 'SMS'; const DONE = ['delivered', 'failed', 'expired', 'cancelled', 'unknown', 'sent, no report']; function onOpen() { SpreadsheetApp.getUi().createMenu('SendHarbor') .addItem('Send new rows', 'sendNewRows') .addItem('Update statuses', 'updateStatuses') .addToUi(); } // Run once from the editor: checks statuses every five minutes. function installStatusTrigger() { ScriptApp.newTrigger('updateStatuses').timeBased().everyMinutes(5).create(); } function settings_() { const props = PropertiesService.getScriptProperties(); const sandbox = props.getProperty('SENDHARBOR_SANDBOX') === 'true'; return { key: props.getProperty('SENDHARBOR_API_KEY'), deviceId: sandbox ? 'sandbox-phone' : props.getProperty('SENDHARBOR_DEVICE_ID'), path: sandbox ? '/api/sandbox/messages' : '/api/messages', }; } function request_(settings, method, path, body) { const options = { method: method, headers: { Authorization: 'Bearer ' + settings.key }, muteHttpExceptions: true, }; if (body) { options.contentType = 'application/json'; options.payload = JSON.stringify(body); } const response = UrlFetchApp.fetch(SENDHARBOR_URL + path, options); let json = {}; try { json = JSON.parse(response.getContentText()); } catch (e) {} return { code: response.getResponseCode(), json: json }; } function statusOf_(message) { const status = message.status || {}; return status.phase === 'sent' && status.deliveryReport === 'unconfirmed' ? 'sent, no report' : status.phase; } function sendNewRows() { const lock = LockService.getScriptLock(); if (!lock.tryLock(30000)) return; try { const settings = settings_(); const sheet = SpreadsheetApp.getActive().getSheetByName(SHEET_NAME); const rows = sheet.getDataRange().getValues(); for (let i = 1; i < rows.length; i++) { const row = i + 1; const phone = String(rows[i][0]).trim(); const text = String(rows[i][1]); if (!phone || !text.trim() || rows[i][3]) continue; if (!/^\+[1-9]\d{6,14}$/.test(phone)) { sheet.getRange(row, 5, 1, 2).setValues([['error: use +15551234567 format', new Date()]]); continue; } let key = rows[i][2]; if (!key) { key = 'sheets-' + Utilities.getUuid(); sheet.getRange(row, 3).setValue(key); SpreadsheetApp.flush(); // The key is saved before sending, so a retry reuses it. } const result = request_(settings, 'post', settings.path, { deviceId: settings.deviceId, phone: phone, text: text, idempotencyKey: key, }); if (result.code === 202) { const message = result.json.message; sheet.getRange(row, 4, 1, 3).setValues([[message.id, statusOf_(message), new Date()]]); } else { const error = (result.json.error && result.json.error.code) || 'request failed'; sheet.getRange(row, 5, 1, 2).setValues([['error ' + result.code + ': ' + error, new Date()]]); } } } finally { lock.releaseLock(); } } function updateStatuses() { const settings = settings_(); const sheet = SpreadsheetApp.getActive().getSheetByName(SHEET_NAME); const rows = sheet.getDataRange().getValues(); let checked = 0; for (let i = 1; i < rows.length && checked < 50; i++) { const id = rows[i][3]; if (!id || DONE.includes(rows[i][4])) continue; checked++; const result = request_(settings, 'get', settings.path + '/' + encodeURIComponent(id)); if (result.code === 200) { sheet.getRange(i + 1, 5, 1, 2).setValues([[statusOf_(result.json.message), new Date()]]); } } }Check: after you reload the sheet, a SendHarbor menu appears in the menu bar.
-
Store the settings
In Apps Script, open Project Settings → Script properties and add these properties:
Property Value SENDHARBOR_API_KEYYour API key. Use a sandbox key for the first test. SENDHARBOR_SANDBOXtrue for the sandbox, false for real SMS. SENDHARBOR_DEVICE_IDThe phone ID. Not needed in the sandbox. Editors of the script can read its properties. Share the sheet only with people who may use this key.
Check: Project Settings lists the properties with no spaces around the values.
-
Send a test row
Add a row with your own number and a short text, then choose SendHarbor → Send new rows. On the first run Google asks you to allow access to the sheet and to external services.
Check: the row gets a key, a message ID and a status such as queued or delivered.
-
Turn on status updates
In the editor, select installStatusTrigger in the function list and click Run once. It creates a timer that runs updateStatuses every five minutes.
Check: the Triggers page lists updateStatuses with a time-driven event.
-
Switch to real SMS
Set SENDHARBOR_SANDBOX to false, add SENDHARBOR_DEVICE_ID and use a key that is not a sandbox key. Send one row to your own number before you send to customers.
Check: the SMS arrives on your phone and the status changes to sent or delivered.
The request the script sends
One request per new row. The body follows the OpenAPI contract.
{
"deviceId": "DEVICE_ID",
"phone": "+15551234567",
"text": "Your appointment is tomorrow at 10:00.",
"idempotencyKey": "sheets-1b4e28ba-2fa1-41d2-883f-0016d3cca427"
}
In the sandbox the script calls the sandbox address with the phone ID sandbox-phone.
{
"deviceId": "sandbox-phone",
"phone": "+15551234567",
"text": "Your appointment is tomorrow at 10:00.",
"idempotencyKey": "sheets-1b4e28ba-2fa1-41d2-883f-0016d3cca427"
}
What the statuses mean
| Status | Meaning |
|---|---|
queued | Accepted into the queue. Sending has not started. |
submitted, sending | Handed to the phone. The result is not known yet. |
sent | The phone sent the SMS. A delivery report has not arrived yet. |
sent, no report | No delivery report within 24 hours. This is not a failure. |
delivered | The carrier reported delivery. |
failed, expired, cancelled | Not sent. Open the message in the workspace to see why. |
unknown | The result cannot be confirmed. SendHarbor does not resend it automatically. |
error 409 | The number or text changed after a send with this key. Clear the Key cell to send it as a new message. |
Limits
- Apps Script has daily quotas for URL Fetch calls and a time limit for each run. The timer checks at most 50 rows per run.
- A web app in Apps Script cannot read request headers, so it cannot check a webhook signature. This recipe reads statuses on a timer instead.
- For hundreds of rows at once, use the batch request described in the API docs.