How to Download a CSV File from Google Spreadsheet Using Google HTML Service in Google Apps Script
Dec 21, 2025
Buy the full source code of application here:
https://buy.stripe.com/eVa9DUe7DaoU7fyeX7
Show More Show Less View Video Transcript
0:00
Uh hello friends today in this tutorial
0:02
I will be telling you that how to
0:04
convert your Google sheet file in into
0:07
CSV file using Google appcript. So this
0:10
is a demo application that we will be
0:11
building. So you can see that this is my
0:14
Google spreadsheet and it contains two
0:16
columns name and marks and it has two
0:18
records Gotham John and 6768. I need to
0:21
convert it in into CSV file and I have
0:24
made a custom menu item here of export.
0:27
Here you can see that there is the
0:29
option of export CSV files. If I hit
0:31
this option, this will run the Google
0:33
app script and this will be the dialogue
0:35
box shown to be that is CSV down
0:38
download dialogue. So if I click this
0:40
okay here, so it will download this CSV
0:43
file in the attachment. You can see that
0:45
file 3 dot CSV. And if I open my Google
0:48
Drive folder, one file will be
0:50
successfully created here. You can see
0:51
that file dot CSV. So I have a CSV
0:55
viewer. Let me just open this file
0:57
inside my CSV viewer. So this is my
0:59
application. If I open this file file 3
1:03
CSV that I have downloaded. If I open
1:05
this you will see that the same thing is
1:07
shown to me. Name marks. Uh first is
1:10
Gotham John 6768. So this is a very
1:13
simple application that in this we will
1:16
be looking that how to convert the
1:18
Google spreadsheet into a CSV file that
1:21
we can download into our computer using
1:23
Google appcript. So let's get started
1:26
for building this. First of all, just go
1:28
to your Google Drive and create a brand
1:30
new folder called as whatever you want.
1:32
I have called it as CSV and let me just
1:35
delete this record and start from
1:37
scratch. Remove this. So first of all,
1:40
you need to go to Google apps Google
1:43
sheet. Just type on Google and the very
1:46
first link which comes in. Click this
1:48
link. We will be creating a blank sheet.
1:50
So here click on go to Google sheet.
1:54
And uh this will be redirected to you
1:57
the page here. So just create a blank
1:59
sheet here.
2:03
So click the blank button here. So
2:05
create a blank sheet. So this will be
2:08
the spreadsheet. You can see that. Let
2:11
me just insert some records here. First
2:13
field will be name.
2:16
Second will be marks let's suppose.
2:20
And let me have two names which is
2:22
Goautam
2:24
60. Let's suppose we have 87 and the
2:27
next record let's suppose Harshad
2:31
and it has uh 83. So now we have two
2:35
records here inside the spreadsheet. And
2:37
now we just need to write our Google
2:39
appcript code. So just go to tools and
2:41
go to script editor. So this will
2:43
redirect you to this uh page here. So
2:46
here you will be writing your Google
2:49
appcript code. So let me just delete all
2:51
the code from here and write a brand new
2:53
function which will be on open. So when
2:56
you open the spreadsheet this will
2:58
execute. So this is a function. So the
3:02
function name must be same. So you
3:04
should name this as on open. So this is
3:06
a standard method. So when you open the
3:08
spreadsheet this method will execute on
3:11
open. So first of all we just need to
3:13
get the reference on the spreadsheet. So
3:16
we can just write spre spreadsheet app
3:19
and uh we can get the UI by invoking a
3:23
get a UI method and inside this we need
3:26
to create a menu. So we will be creating
3:29
a menu here like this by using this
3:32
method and here you can just pass any
3:35
string. Let's suppose I want to have
3:37
export CSV. So this will be the text
3:40
shown to the user and inside the menu we
3:42
will be creating the item. So we will be
3:45
adding the item like this by using this
3:47
function. So you just need to add a
3:49
caption or string. Let's suppose um
3:53
export
3:59
export
4:00
this to CSV like this. And now we just
4:05
need to provide the function name inside
4:07
single quotes as well. So let me just
4:09
close the single quotes here. And here
4:11
you just need to write the function name
4:13
which will be responsible once it wants
4:16
to execute this. So, so here we will be
4:20
writing in single quotes the function
4:22
name that will be responsible. So we
4:24
will be giving this function as
4:27
dialogue. You can just give any name of
4:29
your choice. And lastly we will add this
4:31
to the UI by invoking this simple method
4:35
add to UI. That's it. And once again if
4:38
I just run this function first of all we
4:40
just need to
4:42
uh
4:44
save this. So sheet to CSV. Okay. And
4:48
now it will ask for permission. So when
4:49
you run this for the very first time. So
4:53
let me just select the function on open
4:55
here like this.
4:57
So it will ask for the permission
4:59
because we are accessing spreadsheet
5:01
here. So once you run this. So if you
5:05
run this you can see that one menu item
5:08
will be there. Export CSV. You can see
5:10
that this menu item is there. Export
5:12
this to CSV. And now we just need to
5:14
create a dialogue box. This dialogue
5:16
function we need to create this. So we
5:19
will be writing this function function
5:22
dialogue.
5:27
So inside this function guys we will be
5:29
loading a HTML form here. For this we
5:32
will be declaring HTML variable here. So
5:34
we will be using HTML service once again
5:38
in order to load some HTML. So create we
5:42
will be using create HTML output from
5:44
file.
5:46
So create HTML output from file. And
5:49
here we will be giving it file name to
5:51
it. Let's suppose we give it as
5:54
download.
5:56
And now we just need to create this
5:58
file. So once we create this file we
6:01
will be getting the output.
6:04
So on the next line we just need to
6:06
write here spreadsheet
6:09
app dot get UI
6:14
dot show model. So basically this is a
6:17
function here in order to show model
6:19
dialog boxes. This is a function and
6:22
here you just need to pass the HTML
6:25
which is coming in inside our HTML
6:27
variable.
6:28
Pass the HTML and you need to give it a
6:32
uh title to the dialog box. So let's
6:35
suppose I give it here CSV
6:39
download
6:49
dialogue like this.
6:52
Close the single quotes. And now we just
6:54
need to create this uh download HTML
6:57
file. So create new HTML
7:00
and give it a name download HTML.
7:04
And now inside this HTML guys we just
7:06
need to write a very simple
7:09
uh HTML. Now in in in this we will be
7:12
having a form tag.
7:19
We need to close this. So inside the
7:21
form we will be having a simple input
7:23
which will be type of button.
7:28
input type button and the value will be
7:33
okay. So we will be just asking the user
7:36
that if you want to download it or not
7:38
just close it and once the user clicks
7:41
onto the button so we will be providing
7:42
a onclick event handler. So we will be
7:45
executing this function
7:46
google.script.run
7:49
and uh we will be invoking this method
7:52
with success handler. So just type this
7:56
same because this is a standard function
7:58
in Google appcript.
8:00
So once it is successful it will call
8:03
this method success handler. So here you
8:06
just need to pass uh the method that you
8:09
will execute in the parenthesis. I will
8:12
be executing execute
8:16
download. This method I will be creating
8:18
in the script section. execute, download
8:21
and also
8:26
we will be concatenating another method
8:28
which will be we will be running here
8:33
just after this get file URL.
8:38
So we will be creating this method
8:40
inside the Google app script. So this
8:42
will get the file URL that you want and
8:45
put a semicolon and close the parenthe
8:48
double quotes and now also close this
8:50
button. So now this execute download
8:53
method we will be writing inside the
8:55
script section. So once you get the URL
8:57
you will be downloading it. So in order
8:59
to download this it is very simple.
9:02
Inside the script, we will be writing
9:04
our function execute
9:07
download
9:14
and we will be just be redirecting the
9:15
user. It will be expecting the URL that
9:18
the of the CSV file and now we just need
9:22
to redirect the user. So we can just use
9:25
window.loation.href
9:28
and we will be redirecting it to this
9:30
URL. That's it. So this is the one line
9:32
of code that is required. So now the
9:34
HTML file is complete. And now if I run
9:37
this uh let me just save this
9:44
and uh also save this. So it is running
9:48
here. If I run this function here
9:52
just wait. It is running here
9:58
preparing for execution.
10:04
Uh let me see my internet connection is
10:07
let me just start the internet
10:09
connection. Sorry.
10:30
Just wait guys, I'm starting the
10:32
internet.
10:38
So it is started here. So now if I run
10:40
this on open, it is running this. So
10:42
first of all, it wants to grant the
10:45
permission. So we will be signing in
10:48
into our account.
10:54
So click on the advanced section and go
10:57
to sheet here.
11:01
So click on the allow button.
11:05
So it is preparing for execution. And
11:08
now if I reload this. So if I click this
11:10
button, you will see a dialogue box
11:13
coming here. You can see that CSV
11:15
download dialogue. So you will be seeing
11:18
this dialogue box appearing here. And
11:21
now we just need to write our function.
11:23
Once we ex once we just uh hit this
11:27
button, you can see that
11:30
a button is there inside this form. So
11:32
we just need to execute this get file
11:34
URL method.
11:37
So
11:45
with success handler I think that we
11:47
have made a mistake here. execute
11:49
download dot uh get file URL. So it is
11:54
why the button was not showing I think
11:56
that let me just rerun once again. If I
11:59
run this preparing for execution
12:04
if I hit this
12:12
the button is not showing guys. So let
12:14
me just see what is.
12:21
So this was the original code. So let me
12:23
just uh
12:25
copy this here.
12:30
Copy this to this. So this is my here.
12:34
So let me just remove this and once
12:38
again copy.
12:40
So just pause the video and write this.
12:43
So it is again containing a input type
12:45
of button and the value is okay on click
12:47
is this. If I save this and now if I run
12:50
this once again click this and now if I
12:54
reload this export this to US3
13:17
Let me copy this whole code here.
13:31
We save this. So again rerun this.
13:37
Save also this. Save this. And run this
13:40
now.
13:43
And now if I click this export this to
13:45
CSV.
13:47
So now you can see that this button is
13:48
there. And now if I click this button I
13:50
need to convert this into a CSV file. So
13:53
for that guys you just need to
13:58
just pause the video guys and write this
14:00
code here. I have updated this code
14:02
because earlier on the button was not
14:04
showing. So just pause the video and
14:06
write this code.
14:08
And now inside Google appcript now we
14:10
just need to execute this simple
14:12
function. Once we ex uh hit the uh this
14:15
button here we need to execute this
14:16
function get file URL. So we will be
14:18
writing this code inside Google
14:20
appcript. So inside get file URL. So
14:24
just I will create some space function
14:28
get file URL.
14:36
So first of all in that case guys we
14:38
need to uh give a file name into our CSV
14:42
file here. So we will be creating a
14:44
variable of file name and inside this we
14:47
just need to give a file name to it our
14:49
file. So let me just give it as file dot
14:51
CSV and secondly we just need to provide
14:54
the folder id
14:57
of the Google drive. So in on Google
14:59
drive you have created a folder here. So
15:01
right click it on get sharable link. So
15:04
this will get the sharable link. can now
15:06
just open this inside your you can see
15:08
that it will open this folder inside
15:11
your Google drive. So the ID is very
15:14
simple. This is the ID. You can see that
15:17
copy this ID and here you just need to
15:19
paste it. So inside double quotes paste
15:22
the folder ID. So after getting the
15:25
folder ID, it is very simple. We need to
15:27
create a simple file here.
15:30
So we will be declaring a CSV variable.
15:34
We will be initializing it to nothing.
15:36
And after that we will be getting the
15:38
reference to our spreadsheet
15:41
app
15:46
dot
15:48
active sheet. We will get the active
15:50
sheet here.
15:54
Get active sheet like this.
16:00
And after that we will be invoking
16:02
another method which will be
16:05
oh sorry first of all we need to invoke
16:07
this get active spreadsheet
16:12
and then we just need to invoke get
16:14
active sheet
16:18
and then again we just need to get the
16:20
data inside our
16:24
cells. So we will be invoking this get
16:26
data range and again we just need to
16:29
once again get the values for we will be
16:32
invoking get values here method get
16:34
values. So get values
16:39
select the second method and now after
16:42
this guys we will get the values. So we
16:44
will be now be running a for each loop
16:46
here
16:50
for each loop. And here we just need to
16:52
pass a callback function.
16:55
So this will hold the event object.
16:59
So inside this
17:02
we need to append to the CSV variable
17:04
that we have created. CSV
17:07
let me just use a notation plus is equal
17:10
to
17:13
we will be using E. join
17:17
and inside this parenthesis we will be
17:20
writing in double quotes comma
17:23
and then
17:25
we will be concatenating with the new
17:27
line character so just write in this new
17:30
line like this
17:34
that's it so this will construct the CSV
17:36
file for us so we will be taking each uh
17:40
value inside spreadsheet and then we
17:43
will be joining it by comma. So we will
17:45
be constructing the CSV file like this.
17:48
After constructing it, we just need to
17:50
get the URL of the file. So we will be
17:52
declaring a URL variable and then we
17:54
will be using drive app dot get folder
17:59
by id. So this is a method we will use
18:01
and we will pass the ID of the folder
18:03
which is there inside the folder
18:05
variable. And then we just need to
18:07
create a simple file. We will be using
18:09
the create file method.
18:14
We will be using the second method here
18:17
or third method. So this takes three
18:19
arguments. First is the name of the
18:20
file. We have pass the name inside our
18:25
file name variable. The content will be
18:28
CSV
18:29
will be holding the CSV content. And the
18:32
third time will be third argument is mim
18:34
type. So mim type is whatever is the
18:36
extension. So here you'll be giving mim
18:38
type dot
18:40
sorry this is a constant mim type
18:46
dot csv. So this is a CSV file like
18:49
this. And now after creating the file we
18:52
just need to get the download URL also.
18:55
So we can just invoke a simple method
18:57
here dot get download URL. So basically
19:03
it will return the download URL for us.
19:05
And after that we will be invoking
19:07
another method onto this dot replace.
19:14
And inside parenthesis we will pass this
19:17
string here inside double quotes just
19:19
pass this question mark E is equal to
19:23
download
19:25
and
19:27
GD is equal to true. Just pass this.
19:34
And the second argument put a comma and
19:36
just write double quotes like this
19:39
nothing inside it. So that's it. So this
19:41
construct the URL and now we just need
19:43
to return this URL to our uh function
19:47
which is there inside download HTML. So
19:49
this is the code guys. So now this code
19:52
is complete. If I run this,
19:55
it is saving the file code and now it
19:57
will run this function. So now it will
20:00
require the permissions because we are
20:01
using the Google Drive now. So it will
20:05
be asking for the very first time. So if
20:06
you're running this application, so if
20:08
you run this application for the second
20:10
time and third time now, then it will
20:12
not ask for it. So just uh allow the
20:15
permissions. And now
20:20
if you go to your spreadsheet,
20:23
click this option, export this to CSV,
20:26
it will run the script and there is a
20:28
button. Click on the okay button. And
20:30
now it will be downloading this file.
20:33
You can see that file 4.csv.
20:36
And now if I open this file inside my
20:38
CSV viewer. If I open this software,
20:47
if I click on open, let me open the
20:50
fourth file that we have downloaded. You
20:51
can see that name marks Goautam 87,
20:55
Harshad 83. So basically in this
20:57
tutorial we have seen a very basic
20:59
example that how to convert your Google
21:01
spreadsheet application in into CSV file
21:04
using Google appcript. So, thanks very
21:06
much for watching this
