function generateLink() {
const authorizationToken="Bearer prxuctCRC2vNwxmZfyoHaDlBr49zcPLNpiWRWYOcRJuCB1Yfkq4aXKK3EtD6QsG3Uc9lPoEOZqEK1mtcq4KtcN_w"
const headers= { "Authorization":authorizationToken,"Content-Type": "application/json" }
const getContactUrl= (email)=>https://api.surveysparrow.com/v3/contacts?search=${email}
;
const contactOptipn= { headers };
const uniqueLinkUrl="https://api.surveysparrow.com/v3/channels/create_unique_links";
const uniqueLinkOptions=({contact_ids})=> ({
"method": "post",headers,
"payload": JSON.stringify({
"survey_id": 331637,
"channel_id": 2862987,
contact_ids
})
})
const getEmails = SpreadsheetApp.getActiveSheet().getRange("D:D").getValues()
for(let i=2;i<getEmails.length;i++){
const email=getEmails[i][0]
const cellValue= SpreadsheetApp.getActiveSheet().getRange(E${i+1}
).getValue();
if(!!!email || !!cellValue) continue;
const contactUrl= getContactUrl(email);
const response=UrlFetchApp.fetch(contactUrl,contactOptipn);
console.log("generating survey link for", {email,cellValue})
try{
const id=JSON.parse(response.getContentText()).data[0].id
let uniqueLink=UrlFetchApp.fetch(uniqueLinkUrl,uniqueLinkOptions({contact_ids:[id]}));
uniqueLink=JSON.parse(uniqueLink.getContentText()).data[0].survey_link;
const sheet= SpreadsheetApp.getActiveSpreadsheet();
sheet.getRange(E${i+1}
).setValue(uniqueLink);
}catch(e){
console.log(e.message)
continue;
}
}
}
function addMenu(){
const menu = SpreadsheetApp.getUi().createMenu("Custom");
menu.addItem('Generate Survey Link' ,"generateLink").addToUi();
}
function onOpen(){
addMenu()
}