Class GmailAttachment

Gmail附件

Gmail 附件。這是標準的 Blob,但多了一個 getSize() 方法,比呼叫 getBytes().length 更快,且不會計入 Gmail 讀取配額。

// Logs information about any attachments in the first 100 inbox threads.
const threads = GmailApp.getInboxThreads(0, 100);
const msgs = GmailApp.getMessagesForThreads(threads);
for (let i = 0; i < msgs.length; i++) {
  for (let j = 0; j < msgs[i].length; j++) {
    const attachments = msgs[i][j].getAttachments();
    for (let k = 0; k < attachments.length; k++) {
      Logger.log(
          'Message "%s" contains the attachment "%s" (%s bytes)',
          msgs[i][j].getSubject(),
          attachments[k].getName(),
          attachments[k].getSize(),
      );
    }
  }
}

方法

方法傳回類型簡短說明
copyBlob()Blob傳回這個 Blob 的副本。
getAs(contentType)Blob以轉換為指定內容類型的 Blob 形式,傳回這個物件內的資料。
getBytes()Byte[]取得儲存在這個 Blob 中的資料。
getContentType()String|null取得這個 Blob 中位元組的內容類型。
getDataAsString()String以 UTF-8 編碼取得這個 Blob 的資料做為字串。
getDataAsString(charset)String以指定編碼方式取得這個 Blob 的資料 (字串形式)。
getHash()String取得這個附件的 SHA1 內容雜湊。
getName()String|null取得這個 Blob 的名稱。
getSize()Integer取得這個附件的大小。
isGoogleType()Boolean傳回這個 Blob 是否為 Google Workspace 檔案 (試算表、文件等)。
setBytes(data)Blob設定儲存在這個 Blob 中的資料。
setContentType(contentType)Blob設定這個 Blob 中位元組的內容類型。
setContentTypeFromExtension()Blob根據副檔名,設定這個 Blob 中位元組的內容類型。
setDataFromString(string)Blob從採用 UTF-8 編碼的字串設定這個 Blob 的資料。
setDataFromString(string, charset)Blob從具有指定編碼的字串設定這個 Blob 的資料。
setName(name)Blob設定這個 Blob 的名稱。

內容詳盡的說明文件

copyBlob()

傳回這個 Blob 的副本。

回攻員

Blob:新副本。


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' 也有效。如果是 Google 文件,'text/markdown' 也適用。

回攻員

Blob - 以 Blob 形式呈現的資料。


getBytes()

取得儲存在這個 Blob 中的資料。

回攻員

Byte[]:儲存的位元組數。


getContentType()

取得這個 Blob 中位元組的內容類型。

回攻員

String|null - 這項資料的內容類型 (如果已知),或 null


getDataAsString()

以 UTF-8 編碼取得這個 Blob 的資料做為字串。

回攻員

String:以字串形式呈現的資料。


getDataAsString(charset)

以指定編碼方式取得這個 Blob 的資料 (字串形式)。

參數

名稱類型說明
charsetString將這個 Blob 中的資料編碼為字串時要使用的字元集。

回攻員

String:以字串形式呈現的資料。


getHash()

取得這個附件的 SHA1 內容雜湊。這個方法不會計入 Gmail 讀取配額。

回攻員

String:SHA1 內容雜湊,以字串形式表示。

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getName()

取得這個 Blob 的名稱。

回攻員

String|null:這項資料的名稱 (如已知),或 null


getSize()

取得這個附件的大小。這個方法比呼叫 getBytes().length 更快速,且不會計入 Gmail 讀取配額。

回攻員

Integer:附件大小 (以位元組為單位)。

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

isGoogleType()

傳回這個 Blob 是否為 Google Workspace 檔案 (試算表、文件等)。

回攻員

Boolean - true (如果這個 Blob 是 Google Workspace 檔案);false (如果不是)。


setBytes(data)

設定儲存在這個 Blob 中的資料。

參數

名稱類型說明
dataByte[]新資料。

回攻員

Blob - 這個 Blob,用於鏈結。


setContentType(contentType)

設定這個 Blob 中位元組的內容類型。

參數

名稱類型說明
contentTypeString新的 contentType。

回攻員

Blob - 這個 Blob,用於鏈結。


setContentTypeFromExtension()

根據副檔名,設定這個 Blob 中位元組的內容類型。如果無法從副檔名推測 contentType,則 contentType 為 null

回攻員

Blob - 這個 Blob,用於鏈結。


setDataFromString(string)

從採用 UTF-8 編碼的字串設定這個 Blob 的資料。

參數

名稱類型說明
stringString字串資料。

回攻員

Blob - 這個 Blob,用於鏈結。


setDataFromString(string, charset)

從具有指定編碼的字串設定這個 Blob 的資料。

參數

名稱類型說明
stringString字串資料。
charsetString將字串解讀為位元組時要使用的字元集。

回攻員

Blob - 這個 Blob,用於鏈結。


setName(name)

設定這個 Blob 的名稱。

參數

名稱類型說明
nameString新名稱。

回攻員

Blob - 這個 Blob,用於鏈結。

已淘汰的方法