Miscellaneous
Stay organized with collections
Save and categorize content based on your preferences.
Download and upload a file
function
sendHttpPost
()
{
//
Download
a
file
now
(
GET
),
so
we
can
upload
it
in
the
HTTP
POST
below
.
const
response
=
UrlFetchApp
.
fetch
(
'https://www.google.com/favicon.ico'
);
const
fileBlob
=
response
.
getBlob
();
const
payload
=
{
'fieldOne'
:
'value for field one'
,
'fieldTwo'
:
'value for field two'
,
'fileAttachment'
:
fileBlob
};
//
Because
payload
is
a
JavaScript
object
,
it
will
be
interpreted
as
//
an
HTML
form
.
(
We
do
not
need
to
specify
contentType
;
it
will
//
automatically
default
to
either
'application/x-www-form-urlencoded'
//
or
'multipart/form-data'
)
const
options
=
{
'method'
:
'post'
,
'payload'
:
payload
};
UrlFetchApp
.
fetch
(
'http://example.com/upload_form.cgi'
,
options
);
}
Zip and unzip a file
function
zipUnzip
()
{
const
googleFavIconUrl
=
'https://www.google.com/favicon.ico'
;
const
googleLogoUrl
=
'https://www.google.com/images/srpr/logo3w.png'
;
//
Fetch
the
Google
favicon
.
ico
file
and
get
the
Blob
data
.
const
faviconBlob
=
UrlFetchApp
.
fetch
(
googleFavIconUrl
)
.
getBlob
();
const
logoBlob
=
UrlFetchApp
.
fetch
(
googleLogoUrl
)
.
getBlob
();
//
zip
now
references
a
blob
containing
an
archive
of
both
faviconBlob
and
//
logoBlob
.
const
zip
=
Utilities
.
zip
([
faviconBlob
,
logoBlob
],
'google_images.zip'
);
//
This
will
now
unzip
the
blobs
.
const
files
=
Utilities
.
unzip
(
zip
);
}
Parse a csv file
function
parseCsv
()
{
//
This
will
create
a
2
dimensional
array
of
the
format
//
[[
a
,
b
,
c
],
[
d
,
e
,
f
]]
const
csvString
=
'a,b,c
\n
d,e,f'
;
const
data
=
Utilities
.
parseCsv
(
csvString
);
console
.
log
(
data
);
}
function
formatString
()
{
//
You
can
use
sprintf-like
string
formatting
using
'%'-style
format
//
strings.
//
will
be
:
'123.456000'
Utilities
.
formatString
(
'%11.6f'
,
123.456
);
//
will
be
:
' abc'
Utilities
.
formatString
(
'%6s'
,
'abc'
);
}
base64 encode and decode a string
function
base64Encoding
()
{
//
base64
encoding
is
used
to
encode
binary
data
to
a
text
format
//
you
may
need
to
do
this
if
your
transfer
method
can
't handle spaces,
//
non
-
English
characters
,
etc
.
//
We
'll instantiate a blob here for clarity.
const
blob
=
Utilities
.
newBlob
(
'A string here'
);
//
Let
's return the blob data as a byte[].
const
encoded
=
Utilities
.
base64Encode
(
blob
.
getBytes
());
//
This
will
log
'QSBzdHJpbmcgaGVyZQ=='
console
.
log
(
encoded
);
const
decoded
=
Utilities
.
base64Decode
(
encoded
);
//
This
will
log
the
original
string
.
console
.
log
(
Utilities
.
newBlob
(
decoded
)
.
getDataAsString
());
}
Get a string with the date in the account timezone
function
getDateStringInTimeZone
()
{
//
Default
to
the
current
date
and
time
.
For
a
particular
date
,
pass
a
//
date
string
to
new
Date
()
such
as
//
"February 17, 2016 13:00:00 -0500"
.
Always
include
a
timezone
//
specifier
in
the
date
string
,
or
the
date
string
may
be
interpreted
//
using
a
different
timezone
than
you
intend
.
const
date
=
new
Date
();
//
Default
to
the
account
timezone
.
To
customize
,
see
//
https
:
//
developers
.
google
.
com
/
google
-
ads
/
api
/
reference
/
data
/
codes
-
formats
#timezone-ids
//
e
.
g
.
const
timezone
=
"Europe/Berlin"
const
timeZone
=
AdsApp
.
currentAccount
()
.
getTimeZone
();
//
For
other
formats
,
see
//
https
:
//
developers
.
google
.
com
/
apps
-
script
/
reference
/
utilities
/
utilities
#formatdatedate-timezone-format
//
If
constructing
a
new
date
from
the
returned
date
string
,
be
sure
the
//
format
includes
the
time
zone
letter
'Z'
,
or
the
date
string
may
be
//
interpreted
using
a
different
timezone
than
you
intend
.
const
format
=
'MMM dd, yyyy HH:mm:ss Z'
;
return
Utilities
.
formatDate
(
date
,
timeZone
,
format
);
}
Get date in the past
function
getDateInThePast
()
{
//
Number
of
milliseconds
in
a
day
.
const
MILLIS_PER_DAY
=
1000
*
60
*
60
*
24
;
//
Default
to
the
current
date
and
time
.
For
a
particular
date
,
pass
a
//
date
string
to
new
Date
()
such
as
//
"February 17, 2016 13:00:00 -0500"
.
Always
include
a
timezone
//
specifier
in
the
date
string
,
or
the
date
string
may
be
interpreted
//
using
a
different
timezone
than
you
intend
.
const
date
=
new
Date
();
//
Number
of
days
in
the
past
.
const
numDays
=
3
;
return
new
Date
(
date
.
getTime
()
-
numDays
*
MILLIS_PER_DAY
);
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-20 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-20 UTC."],[[["\u003cp\u003eUtilize the \u003ccode\u003eUrlFetchApp\u003c/code\u003e class to download and upload files, including handling file attachments in HTTP POST requests.\u003c/p\u003e\n"],["\u003cp\u003eEmploy the \u003ccode\u003eUtilities\u003c/code\u003e class to zip and unzip files, enabling efficient management of multiple files within scripts.\u003c/p\u003e\n"],["\u003cp\u003eParse CSV data into a 2-dimensional array using the \u003ccode\u003eUtilities.parseCsv()\u003c/code\u003e function, simplifying data extraction from CSV strings.\u003c/p\u003e\n"],["\u003cp\u003eFormat strings using the \u003ccode\u003eUtilities.formatString()\u003c/code\u003e function, providing control over string representation with format specifiers.\u003c/p\u003e\n"],["\u003cp\u003eEncode and decode data in base64 format with the \u003ccode\u003eUtilities.base64Encode()\u003c/code\u003e and \u003ccode\u003eUtilities.base64Decode()\u003c/code\u003e functions, enabling secure data handling within scripts.\u003c/p\u003e\n"],["\u003cp\u003eGet the current date and time in the user's account timezone and format it according to specified format using the \u003ccode\u003eUtilities.formatDate()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eCalculate and retrieve a date in the past by manipulating the current date and time using milliseconds.\u003c/p\u003e\n"]]],[],null,["# Miscellaneous\n\nDownload and upload a file\n--------------------------\n\n```gdscript\nfunction sendHttpPost() {\n // Download a file now (GET), so we can upload it in the HTTP POST below.\n const response = UrlFetchApp.fetch('https://www.google.com/favicon.ico');\n const fileBlob = response.getBlob();\n\n const payload = {\n 'fieldOne' : 'value for field one',\n 'fieldTwo' : 'value for field two',\n 'fileAttachment': fileBlob\n };\n\n // Because payload is a JavaScript object, it will be interpreted as\n // an HTML form. (We do not need to specify contentType; it will\n // automatically default to either 'application/x-www-form-urlencoded'\n // or 'multipart/form-data')\n\n const options = {\n 'method' : 'post',\n 'payload' : payload\n };\n\n UrlFetchApp.fetch('http://example.com/upload_form.cgi', options);\n}\n```\n\nZip and unzip a file\n--------------------\n\n```gdscript\nfunction zipUnzip() {\n const googleFavIconUrl = 'https://www.google.com/favicon.ico';\n const googleLogoUrl = 'https://www.google.com/images/srpr/logo3w.png';\n\n // Fetch the Google favicon.ico file and get the Blob data.\n const faviconBlob = UrlFetchApp.fetch(googleFavIconUrl).getBlob();\n const logoBlob = UrlFetchApp.fetch(googleLogoUrl).getBlob();\n\n // zip now references a blob containing an archive of both faviconBlob and\n // logoBlob.\n const zip = Utilities.zip([faviconBlob, logoBlob], 'google_images.zip');\n\n // This will now unzip the blobs.\n const files = Utilities.unzip(zip);\n}\n```\n\nParse a csv file\n----------------\n\n```gdscript\nfunction parseCsv() {\n // This will create a 2 dimensional array of the format\n // [[a, b, c], [d, e, f]]\n const csvString = 'a,b,c\\nd,e,f';\n const data = Utilities.parseCsv(csvString);\n console.log(data);\n}\n```\n\nFormat a string\n---------------\n\n```css+lasso\nfunction formatString() {\n // You can use sprintf-like string formatting using '%'-style format\n // strings.\n\n // will be: '123.456000'\n Utilities.formatString('%11.6f', 123.456);\n\n // will be: ' abc'\n Utilities.formatString('%6s', 'abc');\n}\n```\n\nbase64 encode and decode a string\n---------------------------------\n\n```gdscript\nfunction base64Encoding() {\n // base64 encoding is used to encode binary data to a text format\n // you may need to do this if your transfer method can't handle spaces,\n // non-English characters, etc.\n\n // We'll instantiate a blob here for clarity.\n const blob = Utilities.newBlob('A string here');\n\n // Let's return the blob data as a byte[].\n const encoded = Utilities.base64Encode(blob.getBytes());\n\n // This will log 'QSBzdHJpbmcgaGVyZQ=='\n console.log(encoded);\n\n const decoded = Utilities.base64Decode(encoded);\n\n // This will log the original string.\n console.log(Utilities.newBlob(decoded).getDataAsString());\n}\n```\n\nGet a string with the date in the account timezone\n--------------------------------------------------\n\n```gdscript\nfunction getDateStringInTimeZone() {\n // Default to the current date and time. For a particular date, pass a\n // date string to new Date() such as\n // \"February 17, 2016 13:00:00 -0500\". Always include a timezone\n // specifier in the date string, or the date string may be interpreted\n // using a different timezone than you intend.\n const date = new Date();\n\n // Default to the account timezone. To customize, see\n // https://developers.google.com/google-ads/api/reference/data/codes-formats#timezone-ids\n // e.g. const timezone = \"Europe/Berlin\"\n const timeZone = AdsApp.currentAccount().getTimeZone();\n\n // For other formats, see\n // https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format\n // If constructing a new date from the returned date string, be sure the\n // format includes the time zone letter 'Z', or the date string may be\n // interpreted using a different timezone than you intend.\n const format = 'MMM dd, yyyy HH:mm:ss Z';\n return Utilities.formatDate(date, timeZone, format);\n}\n```\n\nGet date in the past\n--------------------\n\n```gdscript\nfunction getDateInThePast() {\n // Number of milliseconds in a day.\n const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;\n\n // Default to the current date and time. For a particular date, pass a\n // date string to new Date() such as\n // \"February 17, 2016 13:00:00 -0500\". Always include a timezone\n // specifier in the date string, or the date string may be interpreted\n // using a different timezone than you intend.\n const date = new Date();\n\n // Number of days in the past.\n const numDays = 3;\n\n return new Date(date.getTime() - numDays * MILLIS_PER_DAY);\n}\n```"]]