Sub topic 11
Importing a CSV from Drive with Apps Script
This chapter assumes you can create a Google Sheet, use Google Drive, identify a CSV file, and write basic spreadsheet formulas. You should also understand that a CSV file stores rows and columns as plain text separated by a delimiter such as a comma.
Apps Script is Google’s JavaScript-based automation environment. It can read a file from Google Drive, convert the file’s text into spreadsheet-shaped data, and write that data into a sheet. This method is useful when you import the same file regularly or need a repeatable process.
How the import works
The script follows this sequence:
The file ID identifies one Drive file. You can find it in the file’s URL. For example, in a URL such as https://drive.google.com/file/d/ABC123/view, the file ID is ABC123.
Create and run the script
Open the destination spreadsheet. Select Extensions > Apps Script, replace the starter code with this function, and replace YOUR_FILE_ID with the ID of your CSV file.
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);
}Save the project and run importCsvFromDrive. The first run asks for permission to access Google Drive and the current spreadsheet. Review the permissions and authorize the script if you trust its source.
The function reads the file as UTF-8 text. Utilities.parseCsv turns that text into a two-dimensional array. Each inner array represents one row, and each item in that row represents one cell. Finally, setValues writes the entire array in one operation.
Writing the complete range at once is usually faster and less error-prone than writing cells inside a loop. The imported sheet must have enough rows and columns for the array. The range dimensions in the example come from the parsed data.
Delimiters and uneven data
parseCsv expects commas by default. If the file uses semicolons, pass the delimiter as a second argument:
const rows = Utilities.parseCsv(csvText, ';');Quoted commas are handled as part of CSV parsing. For example, a field such as "Paris, France" remains one cell rather than becoming two cells.
Every row should normally contain the same number of fields. If rows have different lengths, setValues can fail because the data does not form a rectangular range. Inspect the source file or normalize the rows before writing them when this occurs.
Important data behavior
Apps Script parses the CSV into values, but it does not automatically guarantee that every value has the type you want. After import, inspect dates, identifiers, and numeric fields.
| Data problem | Likely result | Check or correction |
|---|---|---|
| ZIP code `00421` | Leading zeros may disappear after spreadsheet interpretation | Use `=TEXT(A2, "00000")` |
| Extra spaces | Lookups may not match | Use `=ARRAYFORMULA(TRIM(A2:A))` |
| Wrong delimiter | All data may appear in one column | Pass the correct delimiter to `parseCsv` |
| Wrong encoding | Accents or symbols appear garbled | Read the file with its actual encoding |
A file with an empty body can also cause rows[0] to be unavailable. For production automation, add a check before calling getRange. You may also want to delete or rename an existing Imported sheet before creating a new one, but do so carefully because deleting a sheet can remove data.
Verify the imported sheet
After the script runs, compare the imported sheet with the source file:
- Check the expected row and column counts.
- Confirm that the header row has the correct field names.
- Inspect dates and decimal values under the spreadsheet’s locale.
- Search for blank cells where the source contains values.
- Test a lookup against a cleaned text column.
- Confirm that codes and identifiers still have their required formatting.
If the script reports that the file cannot be found, check the file ID and confirm that the authorizing account can access the file. If the import creates one wide column, check the delimiter. If characters are corrupted, identify the file’s encoding and change getDataAsString accordingly.
Exercise
A CSV uses semicolons between fields, but the script places every row into one column. What change should you make?
Show answer
Change Utilities.parseCsv(csvText) to Utilities.parseCsv(csvText, ';'), then run the import again.