Sub topic 3
Leading zeros, dates and other type pitfalls
This chapter assumes you can import a CSV file with File > Import, write basic formulas such as TRIM and TEXT, and read a short Apps Script function. Check the separator settings from the previous chapter before you start.
Leading zeros, dates and type pitfalls
When Sheets imports a delimited file, it converts each value to a number, date or boolean where it can. A ZIP code such as 02139 becomes 2139, and a value such as 3/4 may become a date. Import the column as plain text, or repair it with a formula:
=TEXT(A2, "00000")Dates break when the file's day-month order differs from the spreadsheet locale. For a file that uses DD/MM/YYYY, a value such as 03/04/2026 is read as March 4 in a US locale. Set the locale in File > Settings, or parse the text yourself:
=DATE(RIGHT(A2,4), MID(A2,4,2), LEFT(A2,2))> Note: the original file is the only reliable source. Keep it unchanged before you repair values in the sheet.
Checking and cleaning the imported data
Imported text often carries extra spaces. This formula trims a whole column at once:
=ARRAYFORMULA(TRIM(A2:A))Pulling a CSV from a URL with IMPORTDATA
IMPORTDATA fills the cells below and to the right of the formula with the file's contents. The URL must be public, and the file must be CSV or TSV:
=IMPORTDATA("https://example.com/data.csv")An optional second argument sets the delimiter, for example ";" for semicolon-separated files.
Importing a CSV from Drive with Apps Script
Open Extensions > Apps Script, paste this function, replace the file ID, and run it. You must approve the access request once:
function importCsvFromDrive() {
const fileId = 'YOUR_FILE_ID';
const csvText = DriveApp.getFileById(fileId).getBlob().getDataAsString('UTF-8');
const rows = Utilities.parseCsv(csvText);
const sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet('Imported');
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
}Limits and troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Accented letters appear garbled | Wrong character encoding | Re-import as UTF-8 |
| Day and month are swapped | Locale mismatch | Correct the locale, or use DATE |
| IMPORTDATA shows an error | URL is private or not a CSV | Make the file public, or use Apps Script |
| Rows are missing | Import size limit | Split the file, and check the current Google help page for limits |
Exercise
A ZIP code 02139 imports as 2139 in cell A2. Which formula restores the five-digit text?
Show answer
=TEXT(A2, "00000"). The format string pads the number with leading zeros to five digits.