Created
December 4, 2023 15:46
-
-
Save yohanesgultom/c5c4f2c2fa97361caf665d0e261aea2d to your computer and use it in GitHub Desktop.
! AppScripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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