Skip to content

Instantly share code, notes, and snippets.

@yohanesgultom
Created December 4, 2023 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohanesgultom/c5c4f2c2fa97361caf665d0e261aea2d to your computer and use it in GitHub Desktop.
Save yohanesgultom/c5c4f2c2fa97361caf665d0e261aea2d to your computer and use it in GitHub Desktop.
! AppScripts
const API_URL = 'https://example.com';
const AUTH = 'Basic XXXXXXXXXXXXXXXXXXXXXX';
function getContact(phone) {
const res = UrlFetchApp.fetch(`${API_URL}/contacts/${phone}@c.us`, {
method : 'get',
contentType: 'application/json',
headers : {
Authorization: AUTH,
},
});
return JSON.parse(res.getContentText());
}
function completeData() {
const result = {};
const sheet = SpreadsheetApp.getActive().getSheets()[0];
const range = sheet.getDataRange();
const numRows = range.getNumRows();
// interate all row,
// but skip header row
for (let i = 2; i <= numRows; i++) {
const name = range.getCell(i, 1).getValue();
const year = range.getCell(i, 3).getValue();
const campus = range.getCell(i, 4).getValue();
const phone = range.getCell(i, 8).getValue();
// convert +62XXXXXXXX to 62XXXXXXXX
const waPhone = phone[0] == '+' ? phone.substring(1) : phone;
let waName = result[waPhone];
console.log(`${name} (${campus} ${year}): ${waPhone}`);
if (!result[waPhone]) {
const contact = getContact(waPhone);
waName = contact.name || contact.pushname || contact.email || contact.description || '-';
// cache result
result[waPhone] = waName;
}
// update cell value
range.getCell(i, 10).setValue(waName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment