# Introduction

I’ve been writing a journal in Google Docs for over a year now and I wanted to do some text analysis on what I’ve been writing about. Rather than importing the document as html and formatting the dataframe all in R, I decided to make a Google Docs add-on to export any text document as a CSV file.

## The Data Format

### My Doc

1. Section 1
• Part 1
• Part 2
2. Section 2

Where you will have your text in each section.

To format that for text analysis I want it all in a table that denotes what section the text is under

Title H1 H2 Text
My Doc Section 1 Part 1 Entry 1
My Doc Section 1 Part 2 Entry 2
My Doc Section 2 Entry 3

# Part 1: Basic Functionality

1. onInstall
2. onOpen

onInstall is called when your add-on is first installed. Generally, since the user already has a document open when installing the add-on, onInstall is used to call onOpen so the user doesn’t have to refresh their page to see a menu on install.

function onInstall() {
onOpen();
}


onOpen is called when the document is opened and is used to create the add-on menu. Each menu item calls a function which we will use to run our add-on.

function onOpen() {
var ui = DocumentApp.getUi();

}


# Part 2: Creating the CSV

Step one of getting this document into a CSV file is to create the exportDoc function.

function exportDoc(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
//set default values for each column
var title = h1 = h2 = "";
//create an array to make the csv
var myExport = [];
//define the newline character
var newline = "\r\n";

for (var i = 0; i < paragraphs.length; i++) {
title = paragraphs[i].getText();
break;
h1 = paragraphs[i].getText();
break;
h2 = paragraphs[i].getText();
break;
default:
myExport.push([title, h1, h2, paragraphs[i].getText()])
}
}
Logger.log(myExport)


If you run this with some text in your document, you can check the logs from the script and see the array. To see the logs, go to View > Logs or press Ctrl + Enter from the script screen.

Now we just need to convert this array into CSV format. I’m just going to use a nested loop for now and surround each element with quotes, then combine all elements on a line with comma separation. Place this within the exportDoc function at the end, after the for loop.

//header for csv file.
var csvContent = "data:text/csv;charset=UTF-8,";
csvContent += "Title,H1,H2,Text" + newline;

//loop through each row
myExport.forEach(function(rowArray){
//loop through each element
rowArray.forEach(function(part, index, theArray) {
theArray[index] = '"' +  part + '"';
});
//comma separated elements
var row = rowArray.join(",");
//finish row
csvContent += row + newline;
});
Logger.log(csvContent)


You can run exportDoc again and check the logs to make sure your string is being created.

All that’s left is to download the csvContent as a CSV file. You can give the csvContent through a URL with the following snippet.

var encodedUri = encodeURI(csvContent);
return {
url: encodedUri,
filename: exportName+".csv"
};


<!DOCTYPE html>

<script>
$( document ).ready(function() { // If the user presses the Enter key$('#name').keyup(function(e) {
if (e.keyCode === 13) {
$('#export_id').click(); } }); //run exportDoc function from code script$('#export_id').click(function() {
});

var d = document.createElement('a');
d.href = obj.url;
d.click();
}
});

</script>

<div class="wrapper">
<input type="text" id="name" value="myDocExport" />
<button class="blue" id="export_id">Export</button>
</div>



function exportDataModal() {
.setWidth(300)
.setHeight(113);
DocumentApp.getUi().showModalDialog(html, 'Export as CSV');
}



And modify the menu to run this function.

function onOpen() {
var ui = DocumentApp.getUi();