Google Apps Script β Publish Guide
One-click way to make your master spreadsheet available to FOYSAL IT β even if the sheet stays private. Publish the script below at script.google.com, then paste the URL into Add Data.
1) The Script (paste into Code.gs)
Your master sheet ID is already filled in. Edit DEFAULT_GID if you use a different tab.
/**
* ============================================================
* FOYSAL IT - Google Apps Script Web App
* Serves your lead spreadsheet as CSV / JSON to the FOYSAL IT app.
* Works even if the sheet is PRIVATE (runs with your permission).
* ============================================================
*/
// >>> STEP 1: Your Spreadsheet ID (from the sheet URL)
// https://docs.google.com/spreadsheets/d/ SPREADSHEET_ID /edit
var SPREADSHEET_ID = '1I14GPL_LLCvSUyHT8aU2xDSJAXLxIDuUFweSDUrqWLA';
// >>> STEP 2 (optional): Default sheet tab ID (the "gid" in the sheet URL).
// Leave blank to always use the first tab.
var DEFAULT_GID = '1267128335';
function doGet(e) {
try {
var sheetId = (e && e.parameter && e.parameter.sheetId) || SPREADSHEET_ID;
var ss = SpreadsheetApp.openById(sheetId);
if (!ss) throw new Error('Spreadsheet not found: ' + sheetId);
// ?action=sheets -> list all tabs (used for tab selection)
if (e && e.parameter.action === 'sheets') {
var list = ss.getSheets().map(function (s) {
return {
name: s.getName(),
index: s.getIndex(),
gid: s.getSheetId(),
rows: s.getLastRow(),
cols: s.getLastColumn()
};
});
return json({ ok: true, spreadsheet: ss.getName(), sheets: list });
}
var sheet = pickSheet(ss, e ? e.parameter : {});
var values = sheet.getDataRange().getValues();
if (!values.length) return csv([['(empty sheet)']]);
// ?format=json -> JSON with auto headers
if (e && e.parameter.format === 'json') {
var headers = values[0].map(function (h) { return String(h); });
var rows = values.slice(1).map(function (r) {
var o = {};
headers.forEach(function (h, i) { o[h] = (r[i] == null ? '' : r[i]); });
return o;
});
return json({ ok: true, sheet: sheet.getName(), total: rows.length, rows: rows });
}
// Default: plain CSV (what the FOYSAL IT importer expects)
return csv(values);
} catch (err) {
return json({ ok: false, error: String(err) });
}
}
function pickSheet(ss, params) {
var sheets = ss.getSheets();
if (params.gid) {
var g1 = parseInt(params.gid, 10);
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetId() === g1) return sheets[i];
}
}
if (params.sheet) {
var byName = ss.getSheetByName(params.sheet);
if (byName) return byName;
}
if (DEFAULT_GID) {
var g2 = parseInt(DEFAULT_GID, 10);
for (var j = 0; j < sheets.length; j++) {
if (sheets[j].getSheetId() === g2) return sheets[j];
}
}
return sheets[0];
}
function csv(values) {
var text = values.map(function (r) {
return r.map(escapeCsv).join(',');
}).join('\n');
return ContentService.createTextOutput(text).setMimeType(ContentService.MimeType.CSV);
}
function escapeCsv(v) {
var s = (v == null) ? '' : String(v);
if (s.indexOf('\n') > -1) s = s.replace(/\n/g, ' ');
if (/[",]/.test(s)) s = '"' + s.replace(/"/g, '""') + '"';
return s;
}
function json(obj) {
return ContentService.createTextOutput(JSON.stringify(obj))
.setMimeType(ContentService.MimeType.JSON);
}
2) Publish (Launch) Steps β 7 steps, ~2 minutes
1
Open script.google.com
Log in with the same Google account that owns the spreadsheet. Click βNew projectβ.
2
Paste the code below
Delete the default Code.gs contents, paste the full script from the box below. Press Save (πΎ).
3
Deploy β New deployment
Top-right: click the Deploy button (π) β New deployment.
4
Select type: Web app
Click the gear icon next to Select a type and choose βWeb appβ.
5
Execute as: Me
IMPORTANT β this makes the script read your sheet with YOUR permission (works even if the sheet is private).
6
Who has access: Anyone
IMPORTANT β this lets the FOYSAL IT app call the URL without login. Choose βAnyoneβ.
7
Click Deploy and authorize
Review access β choose your Google account β Advanced β Go to project (unsafe) β Allow. You get a link like https://script.google.com/macros/s/XXXX/exec
3) Test the Published URL
Open these in a browser (replace with your /exec URL). If you see data β it is published correctly.
(your /exec URL)Should display your sheet rows as CSV text in the browser.β¦?action=sheetsLists every tab with its gid β useful to pick the right tab.β¦?format=jsonReturns the sheet as JSON with automatic headers.β¦?gid=1267128335Forces a specific tab by its gid.β οΈ If the URL shows βAccess deniedβ β you deployed with βWho has access: Only meβ. Redeploy: Deploy β Manage deployments β βοΈ Edit β Version: New version β access βAnyoneβ.
If it shows a Google sign-in page β it is fine for browser tests, but the app needs the βAnyoneβ setting.
Never change βExecute asβ to βAnyoneβ β that is not allowed for sheet reads and βMeβ is the correct, safe choice.
If it shows a Google sign-in page β it is fine for browser tests, but the app needs the βAnyoneβ setting.
Never change βExecute asβ to βAnyoneβ β that is not allowed for sheet reads and βMeβ is the correct, safe choice.
4) Pro Advice β Make the Script Powerful & Safe
π Keep it private + Execute as: Me
Never set 'Who has access' to the sheet itself. The Web App runs with YOUR permission, so the spreadsheet can stay fully private. This is the safest way to share data without exposing the sheet.
π Auto-refresh (Cron-like) with a Time-Driven Trigger
In Apps Script: Triggers β Add Trigger β any function (e.g. a refresh cache function) β time-driven β every minute/hour. Useful to keep data fresh without re-importing manually.
π§΅ Watch for new rows (change detection)
Add an onEdit(e) function to log or flag brand-new rows. Combine with a timestamp column so FOYSAL IT always knows which leads are fresh since the last import.
β±οΈ Respect quotas & large sheets
Apps Script has daily quota limits (~6 min compute/day, ~30k cells per getValues). For very big sheets, read in chunks or split into tabs. Our importer already pages in chunks of 500.
π Versioning β Edit, then deploy a NEW version
After changing the code, go to Deploy β Manage deployments β βοΈ Edit β Version: 'New version' β Deploy. The /exec URL stays the same, so FOYSAL IT needs no change.
π§ Email yourself on errors
Add GmailApp.sendEmail('you@gmail.com', ...) inside the catch block so you are alerted the moment the script fails. Add a try/catch to every trigger.
ποΈ Multi-sheet: one script, many tabs
Call the same /exec URL with ?sheet=TabName or ?gid=123 to serve different tabs. Great for separating Leads / Contacts / Clients.
π« Don't paste API keys into the script
Keep secrets out of Apps Script. If you later need a token, use ScriptApp's LockService sparingly, or better β keep keys server-side in FOYSAL IT Integrations, not in the script.
π‘ Recommended master-sheet structure (the importer auto-maps these):
Name Β· Company Β· Email Β· Phone Β· Website Β· Industry Β· Location Β· Notes. Keep one person per row β that is how FOYSAL IT builds one full profile per person.5) Connect to FOYSAL IT
- Copy your URL:
https://script.google.com/macros/s/XXXX/exec - Go to Add Data (π₯ in the sidebar).
- Paste the URL in the Google Sheet box β Preview.
- Check the AI column mapping and row counts β Create Profiles.
Bonus: the script also serves JSON (?format=json) β you can point any other tool at the same URL to read the same master data.