Class Attachment

附件

Google 协作平台附件,例如附加到网页的文件。

请注意,附件是 Blob,可在需要 Blob 输入的任何位置使用。

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]);

方法

方法返回类型简介
deleteAttachment()void删除此附件。
getAs(contentType)Blob以转换为指定内容类型的 blob 形式返回此对象中的数据。
getAttachmentType()AttachmentType返回该附件的类型(HOSTED 或 WEB)。
getBlob()Blob以 blob 的形式返回此对象中的数据。
getContentType()String返回该附件的 MIME 类型。
getDatePublished()Date返回此附件的首次发布日期。
getDescription()String返回该附件的说明。
getLastUpdated()Date返回此附件的上次更新日期。
getParent()Page获取该附件的父页面。
getTitle()String返回该附件的标题。
getUrl()String返回该附件的下载网址。
setContentType(contentType)Attachment设置此附件的 MIME 类型。
setDescription(description)Attachment设置此附件的说明。
setFrom(blob)Attachment设置此附件的实际数据。
setParent(parent)Attachment设置此附件的父页面。
setTitle(title)Attachment设置此附件的标题。
setUrl(url)Attachment设置此附件的下载网址。

详细文档

deleteAttachment()

删除此附件。

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

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getAs(contentType)

以转换为指定内容类型的 blob 形式返回此对象中的数据。此方法会在文件名中添加适当的扩展名,例如 "myfile.pdf"。但是,它假定文件名中最后一个句号后面的部分(如果有)是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”将变为“ShoppingList.12.25.pdf”。

如需查看转化的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能暂时受到更严格的配额约束。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任意一种都有效。

返回

Blob - 数据作为 blob。


getAttachmentType()

返回该附件的类型(HOSTED 或 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");
}

返回

AttachmentType - 附件类型

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getBlob()

以 blob 的形式返回此对象中的数据。

返回

Blob - 数据作为 blob。


getContentType()

返回该附件的 MIME 类型。网络附件运行失败。

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

返回

String - 附件 MIME 类型

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getDatePublished()

返回此附件的首次发布日期。

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

返回

Date - 原始发布日期

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getDescription()

返回该附件的说明。

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

返回

String - 附件说明

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getLastUpdated()

返回此附件的上次更新日期。

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

返回

Date - 上次更新日期

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getParent()

获取该附件的父页面。

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

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

返回

Page - 父页面

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getTitle()

返回该附件的标题。

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

返回

String - 附件标题

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

getUrl()

返回该附件的下载网址。

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

返回

String - 下载网址

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setContentType(contentType)

设置此附件的 MIME 类型。网络附件运行失败。

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

参数

名称类型说明
contentTypeString新的 MIME 类型

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setDescription(description)

设置此附件的说明。

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");

参数

名称类型说明
descriptionString新的广告内容描述

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setFrom(blob)

设置此附件的实际数据。网络附件运行失败。

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);

参数

名称类型说明
blobBlobSource新数据

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setParent(parent)

设置此附件的父页面。

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]);

参数

名称类型说明
parentPage新的家长

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setTitle(title)

设置此附件的标题。

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");

参数

名称类型说明
titleString新标题

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

setUrl(url)

设置此附件的下载网址。仅对网络附件有效。

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");

参数

名称类型说明
urlString

返回

Attachment - 用于链接的附件

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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