/** * AI Layoffs for Google Sheets: =AILAYOFFS(company) beside each company in your sheet. * From https://ailayoffs.org/integrations * * 1. Put your AI Layoffs key on the line below (a free 30-day key is at https://ailayoffs.org/integrations). * 2. Save, go back to the sheet, and type =AILAYOFFS(A2) next to a company's web domain, ticker or name. * It fills four cells: job cuts on record, cuts linked to AI, US WARN notices since 2019, and the latest date. * =AILAYOFFS(A1:A50) fills a whole column at once. */ const AI_LAYOFFS_KEY = "PASTE_YOUR_KEY_HERE"; /** * A company's layoffs on record, from AI Layoffs: job cuts, cuts linked to AI, US WARN notices, latest date. * @param {string|string[][]} company A web domain (salesforce.com), a stock ticker (CRM) or a company name, or a range of them. * @return {Array} One row per company. * @customfunction */ function AILAYOFFS(company) { if (Array.isArray(company)) return company.map(function (row) { return aiLayoffsRow_(row[0]); }); return [aiLayoffsRow_(company)]; } function aiLayoffsRow_(company) { var value = String(company == null ? "" : company).trim(); if (!value) return ["", "", "", ""]; if (!AI_LAYOFFS_KEY || AI_LAYOFFS_KEY === "PASTE_YOUR_KEY_HERE") return ["Add your AI Layoffs key at the top of the script", "", "", ""]; var cache = CacheService.getScriptCache(); var cacheKey = "ailayoffs:" + value.toLowerCase(); var cached = cache.get(cacheKey); if (cached) return JSON.parse(cached); var param = /^[A-Z.]{1,6}$/.test(value) ? "ticker" : /\./.test(value) && !/\s/.test(value) ? "domain" : "name"; var response = UrlFetchApp.fetch("https://ailayoffs.org/api/v1/company?" + param + "=" + encodeURIComponent(value), { headers: { "X-API-Key": AI_LAYOFFS_KEY }, muteHttpExceptions: true, }); var body = JSON.parse(response.getContentText() || "{}"); var row; if (response.getResponseCode() !== 200) row = [body.error || "AI Layoffs could not answer (" + response.getResponseCode() + ")", "", "", ""]; else if (!body.matched) row = [0, 0, 0, "Nothing on record"]; else { var s = body.summary || {}; var latest = [s.latest_job_cut, s.latest_warn_notice].filter(Boolean).sort().pop() || ""; row = [s.job_cuts || 0, s.job_cuts_linked_to_ai || 0, s.warn_notices || 0, latest]; } if (response.getResponseCode() === 200) cache.put(cacheKey, JSON.stringify(row), 21600); return row; }