Created
February 17, 2026 20:13
-
-
Save HubSpotHanevold/e9f07fcdb95898df4e862d8de3c8a4f4 to your computer and use it in GitHub Desktop.
Script for Google Sheet to HubSpot readiness
This file contains hidden or 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
| /** | |
| * Export a Google Sheet tab to CSV, converting multi-select columns | |
| * from comma-separated to semicolon-separated for HubSpot imports. | |
| */ | |
| function exportCsvForHubSpot() { | |
| // ====== SETTINGS ====== | |
| const sheetName = 'Sheet1'; // tab name | |
| const outputFileName = 'hubspot_export.csv'; | |
| const multiSelectColumns = ['Tags', 'Interests']; // header names to convert | |
| const delimiter = ','; // CSV delimiter (usually comma) | |
| // ====================== | |
| const ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| const sheet = ss.getSheetByName(sheetName); | |
| if (!sheet) throw new Error(`Sheet not found: ${sheetName}`); | |
| const values = sheet.getDataRange().getDisplayValues(); // use displayed text | |
| if (values.length === 0) throw new Error('Sheet is empty.'); | |
| const headers = values[0]; | |
| const colIdx = new Set( | |
| multiSelectColumns | |
| .map(h => headers.indexOf(h)) | |
| .filter(i => i >= 0) | |
| ); | |
| if (colIdx.size === 0) { | |
| throw new Error( | |
| `None of the multiSelectColumns headers were found. Found headers: ${headers.join(', ')}` | |
| ); | |
| } | |
| // Transform rows | |
| const transformed = values.map((row, r) => { | |
| if (r === 0) return row; // header row unchanged | |
| return row.map((cell, c) => { | |
| if (!colIdx.has(c)) return cell; | |
| // Convert comma-separated -> semicolon-separated | |
| // Also trims spaces around commas. | |
| // Examples: | |
| // "A, B, C" -> "A;B;C" | |
| // "A,B" -> "A;B" | |
| return String(cell) | |
| .split(',') | |
| .map(s => s.trim()) | |
| .filter(s => s.length > 0) | |
| .join(';'); | |
| }); | |
| }); | |
| // CSV escaping | |
| const csv = transformed | |
| .map(row => row.map(v => csvEscape_(v, delimiter)).join(delimiter)) | |
| .join('\n'); | |
| const file = DriveApp.createFile(outputFileName, csv, MimeType.CSV); | |
| Logger.log(`Created CSV file: ${file.getName()} (${file.getUrl()})`); | |
| } | |
| /** Escape a value for CSV */ | |
| function csvEscape_(value, delimiter) { | |
| const s = value == null ? '' : String(value); | |
| const mustQuote = | |
| s.includes('"') || s.includes('\n') || s.includes('\r') || s.includes(delimiter); | |
| const escaped = s.replace(/"/g, '""'); | |
| return mustQuote ? `"${escaped}"` : escaped; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment