Class Attachment

Attachment

A Sites Attachment such as a file attached to a page.

Note that an Attachment is a Blob and can be used anywhere Blob input is expected.

var filesPage = SitesApp.getSite('example.com', 'mysite').getChildByName("files");
var attachments = filesPage.getAttachments();

// DocsList.createFile accepts a blob input. Since an Attachment is just a blob, we can
// just pass it directly to that method
var file = DocsList.createFile(attachments[0]);

Methods

MethodReturn typeBrief description
deleteAttachment()voidDeletes this attachment.
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getAttachmentType()AttachmentTypeReturn the type of this attachment (HOSTED or WEB).
getBlob()BlobReturn the data inside this object as a blob.
getContentType()StringReturn the mime type of this attachment.
getDatePublished()DateReturn the date this attachment was first published.
getDescription()StringReturn the description of this attachment.
getLastUpdated()DateReturn the date this attachment was last updated.
getParent()PageGet the parent page of this attachment.
getTitle()StringReturn the title of this attachment.
getUrl()StringReturn the download url for this attachment.
setContentType(contentType)AttachmentSet the mime type of this attachment.
setDescription(description)AttachmentSet the descripton of this attachment.
setFrom(blob)AttachmentSet the actual data of this attachment.
setParent(parent)AttachmentSet the parent page of this attachment.
setTitle(title)AttachmentSet the title of this attachment.
setUrl(url)AttachmentSets the download url for this attachment.

Detailed documentation

deleteAttachment()

Deletes this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
attachments[0].deleteAttachment();

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getAs(contentType)

Return the data inside this object as a blob converted to the specified content type. This method adds the appropriate extension to the filename—for example, "myfile.pdf". However, it assumes that the part of the filename that follows the last period (if any) is an existing extension that should be replaced. Consequently, "ShoppingList.12.25.2014" becomes "ShoppingList.12.25.pdf".

Parameters

NameTypeDescription
contentTypeStringthe MIME type to convert to. For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid.

Return

Blob — the data as a blob


getAttachmentType()

Return the type of this attachment (HOSTED or WEB).

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var attType = attachments[0].getAttachmentType();

// This will log "Hosted"
Logger.log(attType);

// Since this returns an AttachmentType and not a String, for the
// purposes of equivalence we want to either do this or cast the
// type to a String
if(attType ==  SitesService.AttachmentType.HOSTED) {
  Logger.log("This is a match");
}

// The above is the same as
if(String(attType) == "Hosted") {
   Logger.log("This is also a match");
}

Return

AttachmentType — the attachment type

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getBlob()

Return the data inside this object as a blob.

Return

Blob — the data as a blob


getContentType()

Return the mime type of this attachment. Fails for web attachments.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var contentType = attachments[0].getContentType();

Return

String — the attachment mime type

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getDatePublished()

Return the date this attachment was first published.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var date = attachments[0].getDatePublished();

Return

Date — the date of original publication

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getDescription()

Return the description of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var description = attachments[0].getDescription();

Return

String — the attachment description

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getLastUpdated()

Return the date this attachment was last updated.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var date = attachments[0].getLastUpdated();

Return

Date — the last updated date

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getParent()

Get the parent page of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();

// This is equal to pages[0]
var parent = attachments[0].getParent();

Return

Page — the parent page

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getTitle()

Return the title of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var title = attachments[0].getTitle();

Return

String — the attachment title

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

getUrl()

Return the download url for this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
var url = attachments[0].getUrl();

Return

String — the download url

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setContentType(contentType)

Set the mime type of this attachment. Fails for web attachments.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
attachments[0].setContentType("text/plain");

Parameters

NameTypeDescription
contentTypeStringthe new mime type

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setDescription(description)

Set the descripton of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();

// This is an example of chaining in action
attachments[0].setTitle("New Title")
              .setDescription("New Description");

Parameters

NameTypeDescription
descriptionStringthe new description

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setFrom(blob)

Set the actual data of this attachment. Fails for web attachments.

var pages = SitesApp.getSite('demositeappsscript').getChildren();
var attachments = pages[0].getAttachments();

// This snippet demonstrates how to create a new text blob and attach it
// to the page
var blob = Utilities.newBlob("This data is now the attachment content");

// We must set a filename since we created the blob from String data
// instead of something like a URL source
blob.setName("attachment.txt");
attachments[0].setTitle("New Title")
              .setDescription("New Description")
              .setContentType("text/plain")
              .setFrom(blob);

Parameters

NameTypeDescription
blobBlobSourcethe new data

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setParent(parent)

Set the parent page of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();
// Sets the parent page to be the next sibling
attachments[0].setParent(pages[1]);

Parameters

NameTypeDescription
parentPagethe new parent

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setTitle(title)

Set the title of this attachment.

var pages = SitesApp.getSite('example.com', 'mysite').getChildren();
var attachments = pages[0].getAttachments();

// This is an example of chaining in action
attachments[0].setTitle("New Title")
              .setDescription("New Description");

Parameters

NameTypeDescription
titleStringthe new title

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

setUrl(url)

Sets the download url for this attachment. Only valid for web attachments.

var pages = SitesApp.getSite('demositeappsscript').getChildren();
var attachments = pages[0].getAttachments();

attachments[0].setTitle("New Web Attachment")
              .setDescription("New Description")
              .setUrl("http://example.com/files/your_file.txt");

Parameters

NameTypeDescription
urlString

Return

Attachment — this Attachment for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://sites.google.com/feeds

Send feedback about...

Apps Script
Apps Script
Need help? Visit our support page.