Class Utilities

公用程式

這項服務提供字串編碼/解碼、日期格式化、JSON 操作,以及其他雜項工作的公用程式。

屬性

屬性類型說明
CharsetCharset
DigestAlgorithmDigestAlgorithm
MacAlgorithmMacAlgorithm
RsaAlgorithmRsaAlgorithm

方法

方法傳回類型簡短說明
base64Decode(encoded)Byte[]將 Base64 編碼的字串解碼為 UTF-8 位元組陣列。
base64Decode(encoded, charset)Byte[]將以 Base64 編碼的字串解碼為特定字元集中的位元組陣列。
base64DecodeWebSafe(encoded)Byte[]將 Base64 網路安全編碼字串解碼為 UTF-8 位元組陣列。
base64DecodeWebSafe(encoded, charset)Byte[]將以 Base64 網路安全方式編碼的字串,解碼為特定字元集中的位元組陣列。
base64Encode(data)String從指定的位元組陣列產生 Base64 編碼字串。
base64Encode(data)String從指定字串產生 base-64 編碼字串。
base64Encode(data, charset)String從特定字元集中的指定字串產生 Base64 編碼字串。
base64EncodeWebSafe(data)String從指定的位元組陣列產生 Base64 網路安全編碼字串。
base64EncodeWebSafe(data)String從指定字串產生 Base64 網路安全編碼字串。
base64EncodeWebSafe(data, charset)String從特定字元集中的指定字串產生 Base64 網路安全編碼字串。
computeDigest(algorithm, value)Byte[]使用指定演算法,對指定 Byte[] 值計算摘要。
computeDigest(algorithm, value)Byte[]使用指定演算法,對指定 String 值計算摘要。
computeDigest(algorithm, value, charset)Byte[]使用指定演算法,對指定 String 值和指定字元集計算摘要。
computeHmacSha256Signature(value, key)Byte[]使用 HMAC-SHA256 和指定金鑰簽署提供的值。
computeHmacSha256Signature(value, key)Byte[]使用 HMAC-SHA256 和指定金鑰簽署提供的值。
computeHmacSha256Signature(value, key, charset)Byte[]使用 HMAC-SHA256 和指定金鑰與字元集,簽署提供的值。
computeHmacSignature(algorithm, value, key)Byte[]使用指定金鑰和值,透過指定演算法計算訊息驗證碼。
computeHmacSignature(algorithm, value, key)Byte[]使用指定金鑰和值,透過指定演算法計算訊息驗證碼。
computeHmacSignature(algorithm, value, key, charset)Byte[]使用指定金鑰和值,透過指定演算法計算訊息驗證碼。
computeRsaSha1Signature(value, key)Byte[]使用 RSA-SHA1 和指定金鑰簽署提供的值。
computeRsaSha1Signature(value, key, charset)Byte[]使用 RSA-SHA1,以指定金鑰和字元集簽署提供的值。
computeRsaSha256Signature(value, key)Byte[]使用指定金鑰,透過 RSA-SHA256 簽署提供的值。
computeRsaSha256Signature(value, key, charset)Byte[]使用指定金鑰,透過 RSA-SHA256 簽署提供的值。
computeRsaSignature(algorithm, value, key)Byte[]使用指定 RSA 演算法和指定金鑰簽署提供的值。
computeRsaSignature(algorithm, value, key, charset)Byte[]使用指定 RSA 演算法、指定金鑰和字元集,簽署提供的值。
formatDate(date, timeZone, format)String根據 Java SE SimpleDateFormat 類別中說明的規格格式化日期。
formatString(template, args)String使用「%」樣式的格式字串,執行類似 sprintf 的字串格式設定。
getUuid()String以字串形式取得 UUID (相當於使用 java.util.UUID.randomUUID() 方法)。
gzip(blob)Blobgzip:壓縮提供的 Blob 資料,並以新的 Blob 物件形式傳回。
gzip(blob, name)Blobgzip:壓縮提供的 Blob 資料,並以新的 Blob 物件形式傳回。
newBlob(data)Blob從位元組陣列建立新的 Blob 物件。
newBlob(data, contentType)Blob從位元組陣列和內容類型建立新的 Blob 物件。
newBlob(data, contentType, name)Blob從位元組陣列、內容類型和名稱建立新的 Blob 物件。
newBlob(data)Blob從字串建立新的 Blob 物件。
newBlob(data, contentType)Blob從字串和內容類型建立新的 Blob 物件。
newBlob(data, contentType, name)Blob從字串、內容類型和名稱建立新的 Blob 物件。
parseCsv(csv)String[][]傳回 CSV 字串的表格 2D 陣列表示法。
parseCsv(csv, delimiter)String[][]使用自訂分隔符號,傳回 CSV 字串的表格 2D 陣列表示法。
parseDate(date, timeZone, format)Date根據 Java Standard Edition SimpleDateFormat 類別中說明的規格,剖析提供的字串日期。
sleep(milliseconds)void休眠指定的毫秒數。
ungzip(blob)Blob解壓縮 Blob 物件,並傳回包含未壓縮資料的 Blob
unzip(blob)Blob[]採用代表 ZIP 檔案的 Blob,並傳回其元件檔案。
zip(blobs)Blob建立新的 Blob 物件,該物件是包含傳入 Blob 資料的 ZIP 檔案。
zip(blobs, name)Blob建立新的 Blob 物件,該物件是包含傳入 Blob 資料的 ZIP 檔案。

內容詳盡的說明文件

base64Decode(encoded)

將 Base64 編碼字串解碼為 UTF-8 位元組陣列。

// This is the base64 encoded form of "Google グループ"
const base64data = 'R29vZ2xlIOOCsOODq+ODvOODlw==';

// This logs:
//     [71, 111, 111, 103, 108, 101, 32, -29, -126, -80,
//      -29, -125, -85, -29, -125, -68, -29, -125, -105]
const decoded = Utilities.base64Decode(base64data);
Logger.log(decoded);

// If you want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

名稱類型說明
encodedString要解碼的資料位元組陣列。

回攻員

Byte[]:以位元組陣列表示的原始資料,由 Base64 編碼的引數表示。


base64Decode(encoded, charset)

將以 Base64 編碼的字串解碼為特定字元集中的位元組陣列。

// This is the base64 encoded form of "Google グループ"
const base64data = 'R29vZ2xlIOOCsOODq+ODvOODlw==';

const decoded = Utilities.base64Decode(base64data, Utilities.Charset.UTF_8);

// This logs:
//     [71, 111, 111, 103, 108, 101, 32, -29, -126, -80,
//      -29, -125, -85, -29, -125, -68, -29, -125, -105]
Logger.log(decoded);

// If you want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

名稱類型說明
encodedString要解碼的資料字串。
charsetCharset指定輸入內容字元集的 Charset

回攻員

Byte[]:以位元組陣列表示的原始資料,由 Base64 編碼的引數表示。


base64DecodeWebSafe(encoded)

將採用 Base64 網路安全編碼的字串解碼為 UTF-8 位元組陣列。

// This is the base64 web-safe encoded form of "Google グループ"
const base64data = 'R29vZ2xlIOOCsOODq-ODvOODlw==';

const decoded = Utilities.base64DecodeWebSafe(base64data);

// This logs:
//     [71, 111, 111, 103, 108, 101, 32, -29, -126, -80,
//      -29, -125, -85, -29, -125, -68, -29, -125, -105]
Logger.log(decoded);

// If you want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

名稱類型說明
encodedString要解碼的網路安全資料位元組陣列。

回攻員

Byte[]:以位元組陣列表示的原始資料,由 Base64 網路安全編碼的引數表示。


base64DecodeWebSafe(encoded, charset)

將 Base64 網路安全編碼字串解碼為特定字元集中的位元組陣列。

// This is the base64 web-safe encoded form of "Google グループ"
const base64data = 'R29vZ2xlIOOCsOODq-ODvOODlw==';

const decoded = Utilities.base64DecodeWebSafe(
    base64data,
    Utilities.Charset.UTF_8,
);

// This logs:
//     [71, 111, 111, 103, 108, 101, 32, -29, -126, -80,
//      -29, -125, -85, -29, -125, -68, -29, -125, -105]
Logger.log(decoded);

// If you want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

名稱類型說明
encodedString要解碼的網路安全資料字串。
charsetCharset指定輸入內容字元的 Charset

回攻員

Byte[]:以位元組陣列表示的原始資料,由 Base64 網路安全編碼的引數表示。


base64Encode(data)

從指定的位元組陣列產生 Base64 編碼字串。Base64 是常見的編碼方式,許多無法接受二進位資料的工具都支援這種編碼。Base64 常見於電子郵件、HTTP 等網際網路通訊協定,或 XML 文件。

// Instantiates a blob here for clarity
const blob = Utilities.newBlob('A string here');

// Writes 'QSBzdHJpbmcgaGVyZQ==' to the log.
const encoded = Utilities.base64Encode(blob.getBytes());
Logger.log(encoded);

參數

名稱類型說明
dataByte[]要編碼的資料位元組陣列。

回攻員

String:以 base64 編碼表示的傳入資料。


base64Encode(data)

從指定字串產生 base-64 編碼字串。Base64 是常見的編碼方式,許多無法接受二進位資料的工具都支援這種編碼。Base64 常見於電子郵件、HTTP 或 XML 文件等網際網路通訊協定。

// Writes 'QSBzdHJpbmcgaGVyZQ==' to the log.
const encoded = Utilities.base64Encode('A string here');
Logger.log(encoded);

參數

名稱類型說明
dataString要編碼的字串。

回攻員

String:輸入字串的 Base64 編碼表示法。


base64Encode(data, charset)

從特定字元集中的指定字串產生 Base64 編碼字串。Charset 是一種字元編碼方式,可將字元編碼。這類資料通常採用二進位格式,可能與特定資料傳輸通訊協定不相容。為確保資料相容性,一般會將資料編碼為 base 64,這是各種無法接受二進位資料的工具普遍接受的編碼。Base64 常見於電子郵件、HTTP 等網際網路通訊協定,或 XML 文件。

// "Google Groups" in Katakana (Japanese)
const input = 'Google グループ';

// Writes "R29vZ2xlIOOCsOODq+ODvOODlw==" to the log
const encoded = Utilities.base64Encode(input, Utilities.Charset.UTF_8);
Logger.log(encoded);

參數

名稱類型說明
dataString要編碼的資料字串。
charsetCharset指定輸入內容字元的 Charset

回攻員

String:以 Base64 編碼表示的輸入字串,並使用指定的 Charset


base64EncodeWebSafe(data)

從指定的位元組陣列產生 Base64 網路安全編碼字串。Base64 是常見的編碼方式,許多無法接受二進位資料的工具都支援這種編碼。Base 64 網頁安全格式通常用於電子郵件、HTTP 或 XML 文件等網際網路通訊協定。

// Instantiates a blob here for clarity
const blob = Utilities.newBlob('A string here');

// Writes 'QSBzdHJpbmcgaGVyZQ==' to the log.
const encoded = Utilities.base64EncodeWebSafe(blob.getBytes());
Logger.log(encoded);

參數

名稱類型說明
dataByte[]要編碼的資料位元組陣列。

回攻員

String:傳入資料的 Base64 網路安全編碼表示法。


base64EncodeWebSafe(data)

從指定字串產生 Base64 網路安全編碼字串。Base64 是常見的編碼方式,許多無法接受二進位資料的工具都支援這種編碼。Base64 網頁安全格式通常用於電子郵件、HTTP 或 XML 文件等網際網路通訊協定。

// Writes 'QSBzdHJpbmcgaGVyZQ==' to the log.
const encoded = Utilities.base64EncodeWebSafe('A string here');
Logger.log(encoded);

參數

名稱類型說明
dataString要編碼的字串。

回攻員

String:輸入字串的 Base64 網路安全編碼表示法。


base64EncodeWebSafe(data, charset)

從特定字元集中的指定字串產生 Base64 網路安全編碼字串。 字元集是一種字元編碼方式,可讓字元編碼。這類作業通常會以二進位格式進行,但這類格式一般與特定資料傳輸通訊協定不相容。為確保資料相容性,一般會將資料編碼為 base 64,這是各種無法接受二進位資料的工具都可接受的常見編碼。Base 64 網頁安全格式通常用於電子郵件、HTTP 或 XML 文件等網際網路通訊協定。

// "Google Groups" in Katakana (Japanese)
const input = 'Google グループ';

// Writes "R29vZ2xlIOOCsOODq-ODvOODlw==" to the log
const encoded = Utilities.base64EncodeWebSafe(input, Utilities.Charset.UTF_8);
Logger.log(encoded);

參數

名稱類型說明
dataString要編碼的資料字串。
charsetCharset指定輸入內容字元的 Charset

回攻員

String:以 Base64 網路安全編碼表示的輸入字串,並使用指定的 Charset


computeDigest(algorithm, value)

使用指定演算法,對指定 Byte[] 值計算摘要。

const input = Utilities.base64Decode(
    'aW5wdXQgdG8gaGFzaA0K');  // == base64encode("input to hash")
const digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
Logger.log(digest);

參數

名稱類型說明
algorithmDigestAlgorithm要使用的 DigestAlgorithm
valueByte[]要計算摘要的輸入字串值。

回攻員

Byte[]:代表輸出摘要的 byte[]。


computeDigest(algorithm, value)

使用指定演算法,對指定 String 值計算摘要。

const digest = Utilities.computeDigest(
    Utilities.DigestAlgorithm.MD5,
    'input to hash',
);
Logger.log(digest);

參數

名稱類型說明
algorithmDigestAlgorithm要使用的 DigestAlgorithm
valueString要計算摘要的輸入字串值。

回攻員

Byte[]:代表輸出摘要的 byte[]。


computeDigest(algorithm, value, charset)

使用指定演算法,對指定 String 值和指定字元集計算摘要。

const digest = Utilities.computeDigest(
    Utilities.DigestAlgorithm.MD5,
    'input to hash',
    Utilities.Charset.US_ASCII,
);
Logger.log(digest);

參數

名稱類型說明
algorithmDigestAlgorithm要使用的 DigestAlgorithm
valueString要計算摘要的輸入字串值。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出摘要的 byte[]。


computeHmacSha256Signature(value, key)

使用指定金鑰,透過 HMAC-SHA256 簽署提供的值。

// This writes an array of bytes to the log.
const input = Utilities.base64Decode(
    'aW5wdXQgdG8gaGFzaA0K');                 // == base64encode("input to hash")
const key = Utilities.base64Decode('a2V5');  // == base64encode("key")
const signature = Utilities.computeHmacSha256Signature(input, key);
Logger.log(signature);

參數

名稱類型說明
valueByte[]要產生雜湊的輸入值。
keyByte[]用於產生雜湊的金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeHmacSha256Signature(value, key)

使用 HMAC-SHA256 和指定金鑰簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeHmacSha256Signature(
    'this is my input',
    'my key - use a stronger one',
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用於產生雜湊的金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeHmacSha256Signature(value, key, charset)

使用 HMAC-SHA256,以指定金鑰和字元集簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeHmacSha256Signature(
    'this is my input',
    'my key - use a stronger one',
    Utilities.Charset.US_ASCII,
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用於產生雜湊的金鑰。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeHmacSignature(algorithm, value, key)

使用指定金鑰和值,透過指定演算法計算訊息驗證碼。

// This writes an array of bytes to the log.
const input = Utilities.base64Decode(
    'aW5wdXQgdG8gaGFzaA0K');                 // == base64encode("input to hash")
const key = Utilities.base64Decode('a2V5');  // == base64encode("key")
const signature = Utilities.computeHmacSignature(
    Utilities.MacAlgorithm.HMAC_MD5,
    input,
    key,
);
Logger.log(signature);

參數

名稱類型說明
algorithmMacAlgorithm用於雜湊輸入值的 MacAlgorithm 演算法。
valueByte[]要產生雜湊的輸入值。
keyByte[]用於產生雜湊的金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeHmacSignature(algorithm, value, key)

使用指定金鑰和值,透過指定演算法計算訊息驗證碼。

// This writes an array of bytes to the log.
const signature = Utilities.computeHmacSignature(
    Utilities.MacAlgorithm.HMAC_MD5,
    'input to hash',
    'key',
);
Logger.log(signature);

參數

名稱類型說明
algorithmMacAlgorithm用於雜湊輸入值的 MacAlgorithm 演算法。
valueString要產生雜湊的輸入值。
keyString用於產生雜湊的金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeHmacSignature(algorithm, value, key, charset)

使用指定金鑰和值,透過指定演算法計算訊息驗證碼。

// This writes an array of bytes to the log.
const signature = Utilities.computeHmacSignature(
    Utilities.MacAlgorithm.HMAC_MD5,
    'input to hash',
    'key',
    Utilities.Charset.US_ASCII,
);
Logger.log(signature);

參數

名稱類型說明
algorithmMacAlgorithm用於雜湊輸入值的 MacAlgorithm 演算法。
valueString要產生雜湊的輸入值。
keyString用於產生雜湊的金鑰。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSha1Signature(value, key)

使用 RSA-SHA1 和指定金鑰簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSha1Signature(
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSha1Signature(value, key, charset)

使用 RSA-SHA1,以指定金鑰和字元集簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSha1Signature(
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
    Utilities.Charset.US_ASCII,
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSha256Signature(value, key)

使用指定金鑰,透過 RSA-SHA256 簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSha256Signature(
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSha256Signature(value, key, charset)

使用指定金鑰,透過 RSA-SHA256 簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSha256Signature(
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
);
Logger.log(signature);

參數

名稱類型說明
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSignature(algorithm, value, key)

使用指定 RSA 演算法和指定金鑰簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSignature(
    Utilities.RsaAlgorithm.RSA_SHA_256,
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
);
Logger.log(signature);

參數

名稱類型說明
algorithmRsaAlgorithm用於雜湊輸入值的 RsaAlgorithm 演算法。
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。

回攻員

Byte[]:代表輸出簽章的 byte[]。


computeRsaSignature(algorithm, value, key, charset)

使用指定 RSA 演算法、指定金鑰和字元集,簽署提供的值。

// This writes an array of bytes to the log.
const signature = Utilities.computeRsaSignature(
    Utilities.RsaAlgorithm.RSA_SHA_256,
    'this is my input',
    PropertiesService.getScriptProperties().getProperty('YOUR_PRIVATE_KEY'),
    Utilities.Charset.US_ASCII,
);
Logger.log(signature);

參數

名稱類型說明
algorithmRsaAlgorithm用於雜湊輸入值的 RsaAlgorithm 演算法。
valueString要產生雜湊的輸入值。
keyString用來產生簽章的 PEM 格式金鑰。
charsetCharset代表輸入字元集的 Charset

回攻員

Byte[]:代表輸出簽章的 byte[]。


formatDate(date, timeZone, format)

根據 Java SE SimpleDateFormat 類別中說明的規格格式化日期。請參閱 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 的規格。

// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
const formattedDate = Utilities.formatDate(
    new Date(),
    'GMT',
    'yyyy-MM-dd\'T\'HH:mm:ss\'Z\'',
);
Logger.log(formattedDate);

參數

名稱類型說明
dateDate要格式化為字串的 Date
timeZoneString結果的輸出時區。
formatString格式符合 SimpleDateFormat 規格。

回攻員

String:格式化字串形式的輸入日期。


formatString(template, args)

使用「%」樣式的格式字串,執行類似 sprintf 的字串格式設定。

// " 123.456000"
Utilities.formatString('%11.6f', 123.456);

// "   abc"
Utilities.formatString('%6s', 'abc');

參數

名稱類型說明
templateString控制傳回內容的格式字串。
argsObject...用來填入範本中「%」預留位置的物件。

回攻員

String:格式化字串。


getUuid()

以字串形式取得 UUID (相當於使用 java.util.UUID.randomUUID() 方法)。這個 ID 無法保證在所有時間和空間中都不重複。因此,請勿在需要保證唯一性的情況下使用。

// This assigns a UUID as a temporary ID for a data object you are creating in
// your script.
const myDataObject = {
  tempId: Utilities.getUuid(),
};

回攻員

String:UUID 的字串表示法。


gzip(blob)

gzip 會壓縮提供的 Blob 資料,並以新的 Blob 物件形式傳回。

const textBlob = Utilities.newBlob(
    'Some text to compress using gzip compression',
);

// Create the compressed blob.
const gzipBlob = Utilities.gzip(textBlob);

參數

名稱類型說明
blobBlobSource要使用 gzip 壓縮的 Blob 物件。

回攻員

Blob:包含壓縮資料的新 Blob


gzip(blob, name)

gzip 會壓縮提供的 Blob 資料,並以新的 Blob 物件形式傳回。這個方法版本可指定檔案名稱。

const textBlob = Utilities.newBlob(
    'Some text to compress using gzip compression',
);

// Create the compressed blob.
const gzipBlob = Utilities.gzip(textBlob, 'text.gz');

參數

名稱類型說明
blobBlobSource要使用 gzip 壓縮的 Blob 物件。
nameString要建立的 gzip 檔案名稱。

回攻員

Blob:包含壓縮資料的新 Blob


newBlob(data)

從位元組陣列建立新的 Blob 物件。許多 Apps Script API 都會使用 Blob,並將二進位資料做為輸入內容。

// Creates a blob object from a byte array.
const data = [71, 79, 79, 71, 76, 69];
const blob = Utilities.newBlob(data);

// Logs the blob data as a string to the console.
console.log(blob.getDataAsString());

參數

名稱類型說明
dataByte[]這個 Blob 的位元組。

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType)

從位元組陣列和內容類型建立新的 Blob 物件。許多 Apps Script API 都會使用 Blob 做為輸入的二進位資料。

// Declares a byte array.
const data = [71, 79, 79, 71, 76, 69];

// Declares the content type of the blob.
const contentType = 'application/json';

// Creates a blob object from the byte array and content type.
const blob = Utilities.newBlob(data, contentType);

// Logs the blob data as a string to the console.
console.log(blob.getDataAsString());

// Logs the content type of the blob to the console.
console.log(blob.getContentType());

參數

名稱類型說明
dataByte[]這個 Blob 的位元組。
contentTypeStringBlob 的內容類型,可以是 null

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType, name)

從位元組陣列、內容類型和名稱建立新的 Blob 物件。許多以二進位資料做為輸入內容的 Apps Script API 都會使用 Blob。

// Declares a byte array.
const data = [71, 79, 79, 71, 76, 69];

// Declares the content type of the blob.
const contentType = 'application/json';

// Declares the name of the blob.
const name = 'Example blob';

// Creates a blob object from the byte array, content type, and name.
const blob = Utilities.newBlob(data, contentType, name);

// Logs the blob data as a string to the console.
console.log('Blob data:', blob.getDataAsString());

// Logs the content type of the blob to the console.
console.log('Blob content type:', blob.getContentType());

// Logs the name of the blob to the console.
console.log('Blob name:', blob.getName());

參數

名稱類型說明
dataByte[]這個 Blob 的位元組。
contentTypeString- Blob 的內容類型 - 可以是 null
nameStringBlob 的名稱,可以是 null

回攻員

Blob:新建立的 Blob。


newBlob(data)

從字串建立新的 Blob 物件。許多 Apps Script API 都會使用 Blob,並將二進位資料做為輸入內容。

// Declares a string for the blob.
const data = 'GOOGLE';

// Creates a blob object from a string.
const blob = Utilities.newBlob(data);

// Logs the blob data in byte array to the console.
console.log('Blob Data:', blob.getBytes());

參數

名稱類型說明
dataString用於 Blob 的字串 (假設為 UTF-8)。

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType)

從字串和內容類型建立新的 Blob 物件。許多 Apps Script API 都會使用 Blob 做為輸入的二進位資料。

// Declares a string for the blob.
const data = 'GOOGLE';

// Declares the content type of blob.
const contentType = 'application/json';

// Creates a blob object from the string and content type.
const blob = Utilities.newBlob(data, contentType);

// Logs the blob data in byte array to the console.
console.log('Blob data:', blob.getBytes());

// Logs the content type of the blob to the console.
console.log(blob.getContentType());

參數

名稱類型說明
dataString用於 Blob 的字串 (假設為 UTF-8)。
contentTypeStringBlob 的內容類型,可以是 null

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType, name)

從字串、內容類型和名稱建立新的 Blob 物件。許多以二進位資料做為輸入內容的 Apps Script API 都會使用 Blob。

// Declares a string for the blob.
const data = 'GOOGLE';

// Declares the content type of the blob.
const contentType = 'application/json';

// Declares the name of the blob.
const name = 'Example blob';

// Create a blob object from the string, content type, and name.
const blob = Utilities.newBlob(data, contentType, name);

// Logs the blob data in byte array to the console.
console.log('Blob data:', blob.getBytes());

// Logs the content type of the blob to the console.
console.log('Blob content type:', blob.getContentType());

// Logs the name of the blob to the console.
console.log('Blob name:', blob.getName());

參數

名稱類型說明
dataString用於 Blob 的字串 (假設為 UTF-8)。
contentTypeStringBlob 的內容類型,可以是 null
nameStringBlob 的名稱,可以是 null

回攻員

Blob:新建立的 Blob。


parseCsv(csv)

傳回 CSV 字串的表格 2D 陣列表示法。

// This creates a two-dimensional array of the format [[a, b, c], [d, e, f]]
const csvString = 'a,b,c\nd,e,f';
const data = Utilities.parseCsv(csvString);

參數

名稱類型說明
csvString字串,其中包含以逗號分隔值 (CSV) 格式表示的單行或多行資料。

回攻員

String[][]:包含 CSV 字串中值的二維陣列。


parseCsv(csv, delimiter)

使用自訂分隔符號,傳回 CSV 字串的表格 2D 陣列表示法。

// This creates a two-dimensional array of the format [[a, b, c], [d, e, f]]
const csvString = 'a\tb\tc\nd\te\tf';
const data = Utilities.parseCsv(csvString, '\t');

參數

名稱類型說明
csvString字串,其中包含以逗號分隔值 (CSV) 格式表示的單行或多行資料。
delimiterChar介於值之間。

回攻員

String[][]:包含 CSV 字串中值的二維陣列。


parseDate(date, timeZone, format)

根據 Java Standard Edition SimpleDateFormat 類別中說明的規格,剖析提供的字串日期。詳情請參閱 Java SimpleDateFormat 類別

// This set of parameters parses the given string as a date in Greenwich Mean
// Time, formatted as year-month-dateThour-minute-second.
const date = Utilities.parseDate(
    '1970-01-01 00:00:00',
    'GMT',
    'yyyy-MM-dd\' \'HH:mm:ss',
);
Logger.log(date);

參數

名稱類型說明
dateString要剖析為日期的字串值。
timeZoneString輸出時區。
formatString日期格式必須符合 SimpleDateFormat 規格。

回攻員

Date:以日期形式輸入的字串。


sleep(milliseconds)

休眠指定的毫秒數。立即讓指令碼休眠指定的毫秒數。允許的最大值為 300000 (或 5 分鐘)。

// Creates a blob object from a string.
const data = 'GOOGLE';
const blob = Utilities.newBlob(data);

// Puts the script to sleep for 10,000 milliseconds (10 seconds).
Utilities.sleep(10000);

// Logs the blob data in byte array to the console.
console.log(blob.getBytes());

參數

名稱類型說明
millisecondsInteger要休眠的毫秒數。

ungzip(blob)

解壓縮 Blob 物件,並傳回包含未壓縮資料的 Blob

const textBlob = Utilities.newBlob(
    'Some text to compress using gzip compression',
);

// Create the compressed blob.
const gzipBlob = Utilities.gzip(textBlob, 'text.gz');

// Uncompress the data.
const uncompressedBlob = Utilities.ungzip(gzipBlob);

參數

名稱類型說明
blobBlobSource壓縮資料的 Blob

回攻員

Blob:代表解壓縮資料的 Blob


unzip(blob)

接受代表 ZIP 檔案的 Blob,並傳回其元件檔案。

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 now unzips the blobs
const files = Utilities.unzip(zip);

參數

名稱類型說明
blobBlobSourceZIP 檔案 Blob。

回攻員

Blob[] - 代表元件 Blob 的 Blob[],每個 Blob 都以 zip 內完整路徑命名。


zip(blobs)

建立新的 Blob 物件,該物件是包含傳入 Blob 資料的 ZIP 檔案。

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

參數

名稱類型說明
blobsBlobSource[]要壓縮的 Blob 陣列。

回攻員

Blob:包含輸入內容的新 Blob,以封存檔形式呈現。


zip(blobs, name)

建立新的 Blob 物件,該物件是包含傳入 Blob 資料的 ZIP 檔案。這個方法版本可指定檔案名稱。

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

參數

名稱類型說明
blobsBlobSource[]要壓縮的 Blob 陣列。
nameString要建立的 ZIP 檔案名稱。

回攻員

Blob:包含輸入內容的新 Blob,以封存檔形式呈現。

已淘汰的方法