Class Utilities

公用程式

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

屬性

屬性類型說明
CharsetCharset
DigestAlgorithmDigestAlgorithm
MacAlgorithmMacAlgorithm
RsaAlgorithmRsaAlgorithm

方法

方法傳回類型簡短說明
base64Decode(encoded)Byte[]將 Base-64 編碼的字串解碼為 UTF-8 位元組陣列。
base64Decode(encoded, charset)Byte[]將 Base-64 編碼的字串解碼為特定字元集的位元組陣列。
base64DecodeWebSafe(encoded)Byte[]將 Base-64 網路安全編碼字串解碼為 UTF-8 位元組陣列。
base64DecodeWebSafe(encoded, charset)Byte[]將 Base-64 網路安全編碼字串解碼為特定字元集的位元組陣列。
base64Encode(data)String從指定的位元組陣列產生 Base-64 編碼的字串。
base64Encode(data)String從指定字串產生 Base-64 編碼字串。
base64Encode(data, charset)String以特定字元集的指定字串產生 Base-64 編碼字串。
base64EncodeWebSafe(data)String從指定的位元組陣列產生 Base-64 網路安全編碼字串。
base64EncodeWebSafe(data)String從指定字串產生 Base-64 網路安全編碼字串。
base64EncodeWebSafe(data, charset)String以特定字元集的指定字串產生 Base-64 網路安全編碼字串。
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 標準版 SimpleDateFormat 類別中所述的規格剖析提供的字串日期。
sleep(milliseconds)void睡眠時間達到指定的毫秒數。
ungzip(blob)Blob解壓縮 Blob 物件,並傳回包含未壓縮資料的 Blob
unzip(blob)Blob[]擷取代表 ZIP 檔案的 Blob,並傳回其元件檔案。
zip(blobs)Blob建立新的 Blob 物件,這個 ZIP 檔案包含傳入的 Blob 資料。
zip(blobs, name)Blob建立新的 Blob 物件,這個 ZIP 檔案包含傳入的 Blob 資料。

內容詳盡的說明文件

base64Decode(encoded)

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

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

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

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

參數

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

回攻員

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


base64Decode(encoded, charset)

將 Base-64 編碼的字串解碼為特定字元集的位元組陣列。

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

var 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 we want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

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

回攻員

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


base64DecodeWebSafe(encoded)

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

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

var 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 we want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

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

回攻員

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


base64DecodeWebSafe(encoded, charset)

將採用 Base-64 網路安全編碼字串解碼為特定字元集的位元組陣列。

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

var 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 we want a String instead of a byte array:
// This logs the original "Google グループ"
Logger.log(Utilities.newBlob(decoded).getDataAsString());

參數

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

回攻員

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


base64Encode(data)

從指定的位元組陣列產生 Base-64 編碼的字串。Base 64 是一種常見的編碼,適用於無法接受二進位資料的各式工具。Base 64 常用於網際網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

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

參數

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

回攻員

String:傳入的資料採用 Base-64 編碼。


base64Encode(data)

從指定字串產生 Base-64 編碼字串。Base 64 是一種常見的編碼,由多種無法接受二進位資料的工具接受。Base 64 通常用於網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

參數

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

回攻員

String:輸入字串的 Base-64 編碼表示。


base64Encode(data, charset)

以特定字元集的指定字串產生 Base-64 編碼字串。字元集是一種編碼字元的方式,使其能夠編碼。這類操作通常以二進位格式完成,通常與某些資料傳輸通訊協定不相容。為使資料相容,這些資料通常會編碼為 Base 64,這是各種無法接受二進位資料的工具接受的常用編碼。Base 64 常用於網際網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

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

參數

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

回攻員

String:具有指定 Charset 的輸入字串以 Base-64 編碼表示。


base64EncodeWebSafe(data)

從指定的位元組陣列產生 Base-64 網路安全編碼字串。Base 64 是一種常見的編碼,由多種無法接受二進位資料的工具接受。Base 64 網路安全通常用於網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

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

參數

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

回攻員

String:傳入的資料採用 Base-64 網路安全編碼表示法。


base64EncodeWebSafe(data)

從指定字串產生 Base-64 網路安全編碼字串。Base 64 是一種常見的編碼,適用於無法接受二進位資料的各式工具。Base 64 網路安全通常用於網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

參數

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

回攻員

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


base64EncodeWebSafe(data, charset)

以特定字元集的指定字串產生 Base-64 網路安全編碼字串。字元集是一種編碼字元的方式,讓字元能夠編碼。這類通訊通常以二進位格式完成,通常與某些資料傳輸通訊協定不相容。為使資料相容,系統通常會將字串編碼為 Base 64,這是許多無法接受二進位資料的工具都接受的常用編碼。Base 64 網路安全常見用於網際網路通訊協定,例如電子郵件、HTTP 或 XML 文件。

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

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

參數

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

回攻員

String:具有指定 Charset 的輸入字串採用 Base-64 網路安全編碼表示法。


computeDigest(algorithm, value)

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

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

參數

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

回攻員

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


computeDigest(algorithm, value)

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

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

參數

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

回攻員

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


computeDigest(algorithm, value, charset)

使用指定的 String 值搭配指定的字元集,計算摘要。

var 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.
var input = Utilities.base64Decode("aW5wdXQgdG8gaGFzaA0K") // == base64encode("input to hash")
var key = Utilities.base64Decode("a2V5"); // == base64encode("key")
var signature = Utilities.computeHmacSha256Signature(input, key);
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeHmacSha256Signature(value, key)

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

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

參數

名稱類型說明
valueString系統會產生雜湊值時使用的輸入值。
keyString要用於產生雜湊值時所用的金鑰。

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeHmacSha256Signature(value, key, charset)

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

// This writes an array of bytes to the log.
var 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[]:代表輸出簽名的位元組 []。


computeHmacSignature(algorithm, value, key)

使用指定鍵和值指定的演算法,計算訊息驗證碼。

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

參數

名稱類型說明
algorithmMacAlgorithm用於對輸入值雜湊處理的 MacAlgorithm 演算法。
valueByte[]系統會產生雜湊值時使用的輸入值。
keyByte[]要用於產生雜湊值時所用的金鑰。

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeHmacSignature(algorithm, value, key)

使用指定鍵和值指定的演算法,計算訊息驗證碼。

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

參數

名稱類型說明
algorithmMacAlgorithm用於對輸入值雜湊處理的 MacAlgorithm 演算法。
valueString系統會產生雜湊值時使用的輸入值。
keyString要用於產生雜湊值時所用的金鑰。

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeHmacSignature(algorithm, value, key, charset)

使用指定鍵和值指定的演算法,計算訊息驗證碼。

// This writes an array of bytes to the log.
var 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[]:代表輸出簽名的位元組 []。


computeRsaSha1Signature(value, key)

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

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSha1Signature("this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n");
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeRsaSha1Signature(value, key, charset)

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

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSha1Signature("this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n"
    Utilities.Charset.US_ASCII);
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeRsaSha256Signature(value, key)

使用特定金鑰的 RSA-SHA256 簽署提供的值。

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSha256Signature("this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n");
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeRsaSha256Signature(value, key, charset)

使用特定金鑰的 RSA-SHA256 簽署提供的值。

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSha256Signature("this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n");
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeRsaSignature(algorithm, value, key)

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

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSignature(Utilities.RsaAlgorithm.RSA_SHA_256,
    "this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n");
Logger.log(signature);

參數

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

回攻員

Byte[]:代表輸出簽名的位元組 []。


computeRsaSignature(algorithm, value, key, charset)

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

// This writes an array of bytes to the log.
var signature = Utilities.computeRsaSignature(Utilities.RsaAlgorithm.RSA_SHA_256,
    "this is my input",
    "-----BEGIN PRIVATE KEY-----\nprivatekeyhere\n-----END PRIVATE KEY-----\n",
    Utilities.Charset.US_ASCII);
Logger.log(signature);

參數

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

回攻員

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.
var 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.
var myDataObject = {
   tempId: Utilities.getUuid();
};

回攻員

String:UUID 的字串表示方式。


gzip(blob)

gzip 會壓縮提供的 Blob 資料,並在新的 Blob 物件中傳回。

var textBlob = Utilities.newBlob("Some text to compress using gzip compression");

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

參數

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

回攻員

Blob:包含壓縮資料的新 Blob


gzip(blob, name)

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

var textBlob = Utilities.newBlob("Some text to compress using gzip compression");

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

參數

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

回攻員

Blob:包含壓縮資料的新 Blob


newBlob(data)

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

// 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,這些 API 會將二進位資料做為輸入。

// 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,這些 API 會將二進位資料做為輸入內容。

// 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,這些 API 會將二進位資料做為輸入內容。

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

參數

名稱類型說明
dataStringblob 的字串,假設為 UTF-8。

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType)

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

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

參數

名稱類型說明
dataStringblob 的字串,假設為 UTF-8。
contentTypeStringblob 的內容類型可以是 null

回攻員

Blob:新建立的 Blob。


newBlob(data, contentType, name)

透過字串、內容類型和名稱建立新的 Blob 物件。許多 Apps Script API 會使用 Blob,這些 API 會將二進位資料做為輸入內容。

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

參數

名稱類型說明
dataStringblob 的字串,假設為 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]]
var csvString = "a,b,c\nd,e,f";
var 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]]
var csvString = "a\tb\tc\nd\te\tf";
var data = Utilities.parseCsv(csvString, '\t');

參數

名稱類型說明
csvString包含單行或多行資料的字串,採用半形逗號分隔值 (CSV) 格式。
delimiterChar在值之間。

回攻員

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


parseDate(date, timeZone, format)

根據 Java 標準版 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.
var 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

var textBlob = Utilities.newBlob("Some text to compress using gzip compression");

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

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

參數

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

回攻員

Blob:代表解壓縮資料的 Blob


unzip(blob)

擷取代表 ZIP 檔案的 Blob,並傳回其元件檔案。

var googleFavIconUrl = "https://www.google.com/favicon.ico";
var googleLogoUrl = "https://www.google.com/images/srpr/logo3w.png";

// Fetch the Google favicon.ico file and get the Blob data
var faviconBlob = UrlFetchApp.fetch(googleFavIconUrl).getBlob();
var logoBlob = UrlFetchApp.fetch(googleLogoUrl).getBlob();

// zip now references a blob containing an archive of both faviconBlob and logoBlob
var zip = Utilities.zip([faviconBlob, logoBlob], "google_images.zip");

// This now unzips the blobs
var files = Utilities.unzip(zip);

參數

名稱類型說明
blobBlobSourceZIP 檔案 blob。

回攻員

Blob[]:代表元件 blob 的 Blob[],每個 Blob 都會以完整路徑命名。


zip(blobs)

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

var googleFavIconUrl = "https://www.google.com/favicon.ico";
var googleLogoUrl = "https://www.google.com/images/srpr/logo3w.png";

// Fetch the Google favicon.ico file and get the Blob data
var faviconBlob = UrlFetchApp.fetch(googleFavIconUrl).getBlob();
var logoBlob = UrlFetchApp.fetch(googleLogoUrl).getBlob();

// zip now references a blob containing an archive of both faviconBlob and logoBlob
var zip = Utilities.zip([faviconBlob, logoBlob]);

參數

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

回攻員

Blob:新的 blob,包含做為封存的輸入內容。


zip(blobs, name)

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

var googleFavIconUrl = "https://www.google.com/favicon.ico";
var googleLogoUrl = "https://www.google.com/images/srpr/logo3w.png";

// Fetch the Google favicon.ico file and get the Blob data
var faviconBlob = UrlFetchApp.fetch(googleFavIconUrl).getBlob();
var logoBlob = UrlFetchApp.fetch(googleLogoUrl).getBlob();

// zip now references a blob containing an archive of both faviconBlob and logoBlob
var zip = Utilities.zip([faviconBlob, logoBlob], "google_images.zip");

參數

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

回攻員

Blob:新的 blob,包含做為封存的輸入內容。

已淘汰的方法