Rank HS catalog rows by keyword overlap against a product description query. The workbook becomes the system of record; Apps Script owns the joins and business rules so analysts are not pasting monthly formulas.
Entry point `lookupHsCodes` reads typed columns with SpreadsheetApp batch APIs. SpreadsheetApp is the primary service; Manual describes how you usually invoke it after authorization.
Keep thresholds (grace days, bands, alert emails, API keys) in Config cells or Script Properties so the same .gs file promotes from sandbox to production without code edits.
This page is technical only: sheet layout, edge cases, runnable code, deploy steps, and FAQs for lookup hs codes from product keywords. No consulting pitch—just the pattern you can paste and harden.
Need this built? Hire a Google Apps Script developer →
Sheet / project setup
| Tab | Columns | Notes |
|---|---|---|
| HsCodes | HS, Description, Chapter | Catalog |
| HsLookup | Ranked | Output |
What this script does
lookupHsCodes() rank HS catalog rows by keyword overlap against a product description query.
- Keyword scoring
- Top-10 matches
- Confidence ratio
Prerequisites
Container-bound script on the workbook that contains the tabs in the setup table. Authorize SpreadsheetApp scopes on first run.
- V8 runtime
- Exact sheet names
- Script timezone = ops timezone
Walkthrough
Tokenize prompt, score catalog description overlap, write top 10 with confidence=score/tokens.
Paste the code, set Config/Properties, run lookupHsCodes once, verify the output tab, then attach a Manual trigger.
Edge cases
No keyword hits yields an empty HsLookup (header only); broaden tokens and retry.
Testing
Use a sandbox copy with 3–5 known rows. Compute expected outputs for lookup hs codes from product keywords offline, run lookupHsCodes, and diff the output tab including one intentional bad row.
Hardening
Add LockService around multi-sheet writes if triggers can overlap. Log run timestamps. Keep API keys and alert emails in PropertiesService—not in shared cells.
Variations
Fork ideas: filter to one business unit, change grain (daily→weekly), or POST a summary payload after lookupHsCodes succeeds.
Operations notes
Assign an owner for the output tab, document regenerate steps, and treat `lookupHsCodes` as source of truth over ad-hoc cell formulas.
Full code: lookupHsCodes()
Run lookupHsCodes when source tabs are current. Edit sheet names and Config/Properties first.
function lookupHsCodes(){
const ui=SpreadsheetApp.getUi();
const q=String(ui.prompt('Product description keywords').getResponseText()).toLowerCase().split(/\\s+/);
const table=SpreadsheetApp.getActive().getSheetByName('HsCodes').getDataRange().getValues().slice(1);
const scored=table.map(r=>{
const text=String(r[1]).toLowerCase();
let score=0; q.forEach(w=>{ if(w && text.indexOf(w)>=0) score++; });
return {hs:r[0], desc:r[1], chapter:r[2], score};
}).filter(x=>x.score>0).sort((a,b)=>b.score-a.score).slice(0,10);
const sh=SpreadsheetApp.getActive().getSheetByName('HsLookup');
sh.clearContents();
sh.appendRow(['HS','Description','Chapter','Score','Confidence']);
const out=scored.map(x=>[x.hs,x.desc,x.chapter,x.score, x.score/q.length]);
if(out.length) sh.getRange(2,1,out.length+1,5).setValues(out);
}- Line 1: Entry point — bind triggers to lookupHsCodes.
- Line 2: Interactive prompts — run from the sheet UI, not headless triggers.
- Line 4: Batch read; avoid per-cell getValue in loops.
- Line 14: Batch write output rows in one setValues call.
- Line 6: Rename sheet constants before production.
Deploy this example
- 01
Open Apps Script
In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.
- 02
Paste and save
Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.
- 03
Authorize once
Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.
- 04
Add the trigger
Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.
Before you run: lookup hs codes from product keywords
- 1Setup tabs exist with headers matching lookupHsCodes
- 2Dry-run on a copy workbook
- 3Timezone verified
- 4SpreadsheetApp authorization completed
- 5Output spot-checked against hand calc
- 6Manual trigger added only after validation
- 7Owners + alert recipients documented
Frequently asked questions
At minimum the tabs listed in the setup table for Lookup HS Codes from Product Keywords. Output tabs may be cleared each run—do not store source-of-truth data there.
Run from the Apps Script editor for dry runs. Production usually uses a Manual trigger after OAuth succeeds once.
No keyword hits yields an empty HsLookup (header only); broaden tokens and retry.
Per-cell calls burn quota and wall time. One read + one write keeps this pattern under the 6-minute execution cap longer.
PropertiesService (Script Properties) for API keys and alert inboxes. Config sheet is fine for non-secret thresholds.
Simple ratios maybe; lookup hs codes from product keywords needs joins, branching, or side effects (email/Docs/API) that Apps Script handles cleanly.
Keep the .gs in clasp/git. Avoid divergent copies of formulas on the output tab—regenerate from the script.