Key Takeaways
- Each guide starts from a free method you can try in Google Sheets before you install anything, so the technique can be checked on your own data before it becomes part of a recurring workflow.
- The Apps Script examples read and write the sheet once.
- Formula alternatives cover the common case while the script handles the awkward edges that arrive with a real export.
- Every example keeps its source columns.
Introduction
File conversion is usually the first step in a data task and often the most error prone one. Postcodes lose their leading zero, long order numbers turn into scientific notation and a comma inside a company name splits a column.
Google Sheets already imports CSV and Excel files. This guide adds the export side: one function that writes a properly escaped CSV file to Drive and one that copies the current workbook into a clean spreadsheet you can download as an Excel file.
What the Two Conversions Do
| Direction | Method | Result |
|---|---|---|
| Sheet to CSV | Apps Script reads the active sheet and writes a Drive file | A .csv file with quoted cells |
| Sheet to Excel workbook | Apps Script copies the spreadsheet | A new Sheets workbook, downloadable as .xlsx |
| CSV to chart | Import the CSV, then Insert then Chart | A chart linked to the imported range |
The second row is deliberate. Apps Script cannot save a native Excel file, but a Google Sheets workbook downloads cleanly as one. That keeps the conversion inside the tools you already have.
The Ready-Made Apps Script
Open Extensions then Apps Script, paste the code below and save it. Run sheetToCsvFile() to create a CSV file from the active sheet. Run workbookCopyForExcel() to create a separate workbook you can download.
/** Escapes one cell for CSV output. */
function csvCell_(value) {
const text = String(value === null || value === undefined ? "" : value);
if (/[",\n]/.test(text)) {
return '"' + text.replace(/"/g, '""') + '"';
}
return text;
}
/** Writes the active sheet to a CSV file in Google Drive. */
function sheetToCsvFile() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getDataRange().getValues();
if (values.length < 2) {
throw new Error("Add a header row and at least one data row first.");
}
const lines = values.map((row) => row.map(csvCell_).join(","));
const name = sheet.getName() + ".csv";
const file = DriveApp.createFile(name, lines.join("\r\n"));
console.log("CSV file created: " + file.getUrl());
}
/** Copies the active spreadsheet into a separate workbook for Excel download. */
function workbookCopyForExcel() {
const book = SpreadsheetApp.getActiveSpreadsheet();
const copy = book.copy("Excel export - " + book.getName());
console.log("Workbook copy created: " + copy.getUrl());
}
The escape function is the important part. A cell is wrapped in quotes only when it contains a comma, a quote or a newline. A quote inside the cell is doubled. That is enough to keep a CSV readable by Excel, a CRM and a script.
Set a column to plain text before export if it holds a postcode or a long identifier. Apps Script writes the displayed value, so a cell that Sheets has already converted to a number cannot be recovered by the export function.
A Worked Example
| Cell value | CSV output | Why |
|---|---|---|
| Isaac Newton | Isaac Newton | No special characters |
| Acme, Ltd | "Acme, Ltd" | Comma requires quotes |
| Value with a quote | Wrapped and doubled | Inner quotes are doubled |
| 07540 123456 | 07540 123456 | Kept as text when the cell is plain text |
The quoted examples show why a hand-built join with commas is risky. The escape function handles those cases before the file is written.
Chart the Imported CSV
Once the CSV is in a sheet, a chart takes a few clicks. Select the range, choose Insert then Chart and pick the chart type in the editor. A column chart suits counts by category, while a line chart suits a value over time.
If the data changes, keep the chart linked to the range rather than pasting a static image. For a repeatable report, put the source range on its own tab and point the chart at that tab.
Where the Conversion Stops
A CSV file has no formatting, formulas or multiple sheets. Only the active sheet is exported and only its values are written. That is the point of the format, but it surprises people who expect colours and column widths to travel with it.
A workbook copy keeps every tab. If you only want one tab in the download, delete the others from the copy before you download. If you want a single CSV per tab, run the export function once on each sheet.
For a more detailed comparison workflow, the CSV diff guide shows how to list added, removed and changed rows after both files are in the same spreadsheet.
Export Without Surprises
Import is easy because Google Sheets handles it for you. Export is where the small details live: quotes, leading zeros and the difference between a sheet and a workbook. Use the two functions to produce files you can hand to someone else without a cleanup step on their side.
When the exported file needs cleaning before it goes anywhere, Flookup Data Wrangler gives you the matching, standardisation and review tools in the same sheet.
Frequently Asked Questions
Does CSV to Excel mean a real xlsx file?
Not directly. The script copies the spreadsheet into a new Google Sheets workbook. Use File then Download to save an Excel workbook from it.
How are cells with commas handled?
The script quotes a cell that contains a comma, a quote or a newline and doubles any quote inside it, which is the standard CSV escape rule.
Where is the CSV file created?
In the root of your Google Drive. Open the log after the run to find the file URL.
Can I export one tab instead of the whole workbook?
Yes. Put the data on the active sheet and run sheetToCsvFile. It reads the active sheet only.
Do I need Apps Script to make a chart?
No. Select the data and use Insert then Chart. The script is only needed to build the export files.