A Google Apps Script that automatically syncs CSV files from a Google Drive folder into a Google Sheet. Features a progress dialog, duplicate detection, and optional cleanup of removed files.
- Auto-import on open – Imports new CSV files every time you open the spreadsheet
- Recursive folder search – Searches subfolders with regex pattern filtering
- Progress dialog – Shows which file is being processed in real-time
- Duplicate detection – Skips files that have already been imported
- Sync deletions – Optionally removes rows when source CSV files are deleted
- Selective columns – Import all columns or only the ones you need
- Header preservation – Protect rows at the top of your sheet from being overwritten
- Atomic writes – All changes are written in a single operation, triggering formula recalculation only once
├── Code.gs # Main script logic
└── ProgressDialog.html # Modal dialog UI
- Open your Google Sheet
- Go to Extensions → Apps Script
- Delete any existing code in
Code.gs - Copy the contents of
Code.gsfrom this repo and paste it
- In the Apps Script editor, click + next to "Files"
- Select HTML
- Name it exactly:
ProgressDialog(without .html extension) - Copy the contents of
ProgressDialog.htmlfrom this repo and paste it
Edit the CONFIG object at the top of Code.gs:
const CONFIG = {
CSV_FOLDER_ID: 'your-folder-id-here',
CSV_FILE_REGEX: '\\.csv$',
CSV_START_ROW: 2,
CSV_DELIMITER: ',',
CSV_COLS_TO_INCLUDE: [1, 2, 4, 5, 10, 12],
CSV_LOCALE: 'en_US',
SHEET_NAME: 'Data',
SHEET_START_ROW: 2,
SYNC_DELETIONS: true,
};| Option | Description |
|---|---|
CSV_FOLDER_ID |
Google Drive folder ID containing your CSV files. Find it in the folder's URL: drive.google.com/drive/folders/[FOLDER_ID] |
CSV_FILE_REGEX |
Regex pattern to filter which CSV files to import. Matches against the relative path from the root folder. See examples below. |
CSV_START_ROW |
Row number to start reading from in each CSV file (1-indexed). Set to 2 to skip a header row, 3 to skip both header and second row, 1 to import all rows. |
CSV_DELIMITER |
Character separating values in your CSV files: ',', ';', or '\t' |
CSV_COLS_TO_INCLUDE |
Array of column numbers to import (1 = first column, 2 = second column, etc). Set to null or [] to import all columns. |
CSV_LOCALE |
Locale code matching your CSV files' number format (e.g., 'en_US' for period as decimal separator, 'sv_SE' for comma). If this differs from the spreadsheet's locale, the spreadsheet locale is temporarily switched during import to ensure correct parsing of numbers, percentages, and currencies. A warning is shown in the dialog when this happens. |
SHEET_NAME |
Name of the sheet tab where data will be imported. Case-sensitive. |
SHEET_START_ROW |
First row in the sheet where data will be written. Use this to preserve header rows or other content at the top. For example, set to 2 to keep row 1 for headers, or 7 to preserve rows 1–6. |
SYNC_DELETIONS |
Set true to remove rows when their source CSV is deleted from the folder. |
| Pattern | Description |
|---|---|
\\.csv$ |
All CSV files in all folders (default) |
^[^/]*\\.csv$ |
Only CSV files in the root folder (no subfolders) |
Diagram\\.csv$ |
Files ending with "Diagram.csv" |
^[^/]+/Diagram\\.csv$ |
Files named exactly "Diagram.csv" in immediate subfolders only |
/Reports/ |
Files in any folder named "Reports" |
^2024/ |
Files in subfolders starting with "2024" |
^Project1/.*\\.csv$ |
All CSV files under the "Project1" folder |
Note: Column A in your sheet is reserved for the source file path (relative to the root folder, e.g.,
Reports/2024/data.csv). Your CSV data starts in column B.
Press Ctrl+S (Windows) or Cmd+S (Mac) and wait for "Project saved" confirmation.
Simple triggers can't show modal dialogs due to Google's security restrictions. You need an installable trigger:
- In the Apps Script editor, click the clock icon in the left sidebar
- Click + Add Trigger (bottom right)
- Configure:
- Choose which function to run:
onOpenTrigger - Choose which deployment:
Head - Select event source:
From spreadsheet - Select event type:
On open
- Choose which function to run:
- Click Save
- Authorize when prompted
The first time you use the script, you need to authorize access to Google Drive:
- In the Apps Script editor, select
importNewCSVFilesfrom the function dropdown - Click Run
- If prompted, click through the authorization dialog and grant permissions
- You only need to do this once per spreadsheet
Reload your spreadsheet (F5 or Ctrl+R / Cmd+R).
The import dialog should appear automatically. Note that it may take a few seconds after page load.
- On spreadsheet open, the trigger checks if the spreadsheet's locale differs from
CSV_LOCALE - If different, the locale is switched to
CSV_LOCALE(this reloads the page) - After reload (or if no switch was needed), a modal dialog is displayed
- If locale was changed, a warning is shown explaining how to restore it
- The dialog clears any stale status, then starts the import process
- The dialog polls for status updates while import runs
- The script recursively scans the configured Drive folder and subfolders for CSV files matching
CSV_FILE_REGEX - Existing data is loaded into memory
- If
SYNC_DELETIONSis enabled, rows from deleted files are filtered out (in memory) - New CSV files are parsed and added to the data (in memory)
- All data is written to the sheet in a single atomic operation
- Formula recalculation triggers once, after all data is in place
- The dialog shows a summary and a Close button
- When the user clicks Close, the original locale is restored (triggering another page reload)
The script is optimized for large datasets:
- Single atomic write – All changes (deletions + additions) are combined and written in one
setValues()call, ensuring formulas recalculate only once - In-memory processing – Data filtering and merging happens in memory, minimizing API calls
- Batch operations – No loops with individual cell writes
- Memory: Very large sheets (approaching 10 million cells) may cause memory issues
- Execution time: Google Apps Script has a 6-minute timeout
- Permissions: The script needs access to Google Drive and Sheets