/** * Domailfix for Google Sheets: verify email addresses and find work emails in your sheet. * * Install (2 minutes): * 1. In your Google Sheet: Extensions > Apps Script. * 2. Delete what is there, paste this whole file, and click Save. * 3. Reload the sheet. A "Domailfix" menu appears. * 4. Domailfix > Set API key, and paste the API key from your Domailfix account page. * * Use: * Verify: select the cells with email addresses (one column), then Domailfix > Verify selected emails. * Results go into the 5 columns to the right: status, reason, score, accept-all score, suggestion. * Find: select two columns, full name and company website, then Domailfix > Find emails. * Results go into the 3 columns to the right: email, result, score. * Rows that already have a result are skipped, so you can run it again to continue a long list. * Each check uses your Domailfix credits (Unknown and Not found are free). */ var DOMAILFIX_URL = "https://domailfix.achivix.com"; var TIME_LIMIT_MS = 5 * 60 * 1000; // Google stops scripts at 6 minutes; stop safely before that function onOpen() { SpreadsheetApp.getUi().createMenu("Domailfix") .addItem("Verify selected emails", "domailfixVerifySelection") .addItem("Find emails (name, website)", "domailfixFindSelection") .addSeparator() .addItem("Set API key", "domailfixSetKey") .addItem("Help", "domailfixHelp") .addToUi(); } function domailfixSetKey() { var ui = SpreadsheetApp.getUi(); var r = ui.prompt("Domailfix API key", "Paste the API key from your Domailfix account page (it starts with dmf_).", ui.ButtonSet.OK_CANCEL); if (r.getSelectedButton() !== ui.Button.OK) return; var key = r.getResponseText().trim(); if (key.indexOf("dmf_") !== 0) { ui.alert("That does not look like a Domailfix API key. It starts with dmf_."); return; } PropertiesService.getUserProperties().setProperty("DOMAILFIX_KEY", key); ui.alert("Saved. Select your email cells and choose Domailfix > Verify selected emails."); } function domailfixHelp() { SpreadsheetApp.getUi().alert( "Verify: select one column of email addresses, then Domailfix > Verify selected emails.\n\n" + "Find: select two columns (full name, company website), then Domailfix > Find emails.\n\n" + "Results are written to the columns on the right. Run again to continue a long list.\n\n" + "Help: " + DOMAILFIX_URL + "/integrations"); } function domailfixKey_() { var key = PropertiesService.getUserProperties().getProperty("DOMAILFIX_KEY"); if (!key) throw new Error("Set your API key first: Domailfix > Set API key."); return key; } function domailfixCall_(method, path, payload) { var opts = { method: method, muteHttpExceptions: true, headers: { "X-API-Key": domailfixKey_() } }; if (payload) { opts.contentType = "application/json"; opts.payload = JSON.stringify(payload); } var resp = UrlFetchApp.fetch(DOMAILFIX_URL + path, opts); var data = {}; try { data = JSON.parse(resp.getContentText()); } catch (e) {} var code = resp.getResponseCode(); if (code === 402) throw new Error("Out of credits. Buy more on your Domailfix account page, then run again."); if (code === 401) throw new Error("The API key is not valid. Domailfix > Set API key."); if (code === 429) { Utilities.sleep(20000); return domailfixCall_(method, path, payload); } if (code >= 400) throw new Error(data.error || ("Domailfix error " + code)); return data; } function domailfixVerifySelection() { var started = Date.now(); var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getActiveRange(); var rows = range.getNumRows(), col = range.getColumn(), row0 = range.getRow(); var emails = range.getValues(); var out = sheet.getRange(row0, col + 1, rows, 5); var current = out.getValues(); var done = 0, skipped = 0; for (var i = 0; i < rows; i++) { var email = String(emails[i][0] || "").trim(); if (!email || email.indexOf("@") < 0 || current[i][0]) { skipped++; continue; } if (Date.now() - started > TIME_LIMIT_MS) { SpreadsheetApp.getActive().toast("Paused to stay within Google's time limit. Run Verify again to continue.", "Domailfix", 10); return; } var r = domailfixCall_("get", "/api/verify?email=" + encodeURIComponent(email)); sheet.getRange(row0 + i, col + 1, 1, 5).setValues([[ r.status || "", r.reason || "", r.score == null ? "" : r.score, r.accept_all_score == null ? "" : r.accept_all_score, r.did_you_mean || ""]]); done++; if (done % 10 === 0) SpreadsheetApp.flush(); } SpreadsheetApp.getActive().toast(done + " checked" + (skipped ? ", " + skipped + " skipped" : "") + ".", "Domailfix", 6); } function domailfixFindSelection() { var started = Date.now(); var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getActiveRange(); if (range.getNumColumns() < 2) { SpreadsheetApp.getUi().alert("Select two columns: full name and company website."); return; } var rows = range.getNumRows(), col = range.getColumn(), row0 = range.getRow(); var values = range.getValues(); var outCol = col + range.getNumColumns(); var current = sheet.getRange(row0, outCol, rows, 3).getValues(); var done = 0; for (var i = 0; i < rows; i++) { var name = String(values[i][0] || "").trim(), site = String(values[i][1] || "").trim(); if (!name || !site || current[i][1]) continue; if (Date.now() - started > TIME_LIMIT_MS) { SpreadsheetApp.getActive().toast("Paused to stay within Google's time limit. Run Find again to continue.", "Domailfix", 10); return; } var r = domailfixCall_("post", "/api/find", { name: name, website: site }); sheet.getRange(row0 + i, outCol, 1, 3).setValues([[r.email || "", r.status || "", r.score || ""]]); done++; if (done % 5 === 0) SpreadsheetApp.flush(); } SpreadsheetApp.getActive().toast(done + " searched.", "Domailfix", 6); }