Skip to content

Instantly share code, notes, and snippets.

@mtabo
Last active October 23, 2024 17:37
Show Gist options
  • Select an option

  • Save mtabo/13c18cd64364d087a3fae234daf0eddc to your computer and use it in GitHub Desktop.

Select an option

Save mtabo/13c18cd64364d087a3fae234daf0eddc to your computer and use it in GitHub Desktop.
App Script Script for Google Sheets that forms the url to send messages through WhatsApp Web from a phone number in a column and a message in another column of the same row.
// Crea el Menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('WhatsApp')
.addItem('Enviar Mensaje a Fila Seleccionada', 'sendWhatsAppMessageToSelectedRow')
.addToUi();
}
function sendWhatsAppMessageToSelectedRow() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Define las columnas donde están los datos (Columna A = 1, Columna B = 2, etc)
var phoneNumberColumn = 3;
var messageColumn = 4;
// Obtiene el rango que está seleccionado
var selectedRange = sheet.getActiveRange();
// Obtiene el número de fila que está seleccionada
var row = selectedRange.getRow();
// Obtiene el valor de las columnas de la fila seleccionada
var phoneNumber = sheet.getRange(row, phoneNumberColumn).getValue();
var message = sheet.getRange(row, messageColumn).getValue();
// Convierte el mensaje a UTF-8 explícitamente
var utf8Message = Utilities.newBlob(message).getDataAsString('UTF-8');
// Codifica el mensaje para la URL
var encodedMessage = encodeURIComponent(utf8Message);
// Construye la URL de WhatsApp
var whatsappUrl = "https://api.whatsapp.com/send?phone=" + phoneNumber + "&text=" + encodedMessage;
// Muestra un cuadro de diálogo que abre WhatsApp Web para el número y mensaje seleccionados
Logger.log(whatsappUrl);
var htmlOutput = HtmlService.createHtmlOutput('<script>window.open("' + whatsappUrl + '");google.script.host.close();</script>')
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Enviando Mensaje');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment