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 Web 安全编码字符串解码为特定字符集的字节数组。
base64Encode(data)String该函数根据给定的字节数组生成 base-64 编码的字符串。
base64Encode(data)String根据给定的字符串生成 base-64 编码的字符串。
base64Encode(data, charset)String该函数可根据特定字符集中的指定字符串,生成 base-64 编码的字符串。
base64EncodeWebSafe(data)String根据指定的字节数组生成 base-64 Web 安全编码字符串。
base64EncodeWebSafe(data)String根据给定的字符串生成 base-64 网络安全编码字符串。
base64EncodeWebSafe(data, charset)String该函数根据特定字符集中的指定字符串,生成一个 base-64 Web 安全编码字符串。
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 字符串的表格二维数组表示形式。
parseCsv(csv, delimiter)String[][]返回使用自定义分隔符的 CSV 字符串的表格二维数组表示形式。
parseDate(date, timeZone, format)Date根据 Java 标准版 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)

将 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 是各种无法接受二进制数据的工具可接受的通用编码。Base64 通常用于电子邮件、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 Web 安全编码字符串。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)

该函数可根据特定字符集中的指定字符串,生成 Base64、Web 安全编码的字符串。 字符集是一种对字符进行编码,以便能够编码的一种方式。这些传输通常以二进制格式完成,通常与某些数据传输协议不兼容。为使数据兼容,系统通常将其编码为 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 Web 安全编码表示法。


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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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[] - 表示输出签名的 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() 方法)。此标识符不一定在所有时间和空间中都是唯一的。因此,请勿在需要保证唯一性的情况下使用。

//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 对象。Blob 用于许多将二进制数据作为输入的 Apps Script 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 对象。Blob 用于许多将二进制数据作为输入的 Apps Script 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 对象。Blob 用于许多将二进制数据作为输入的 Apps 脚本 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 的字节。
contentTypeStringblob 的内容类型可以是 null
nameStringblob 的名称可以是 null

弃踢回攻

Blob - 新创建的 Blob。


newBlob(data)

从字符串创建一个新的 Blob 对象。Blob 用于许多将二进制数据作为输入的 Apps Script 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 对象。Blob 用于许多将二进制数据作为输入的 Apps Script 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 对象。Blob 用于许多将二进制数据作为输入的 Apps 脚本 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 字符串的表格二维数组表示形式。

// 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 字符串的表格二维数组表示形式。

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

休眠指定的毫秒数。立即将脚本置于休眠状态,并持续指定的毫秒数。允许的最大值为 300,000(即 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 内的完整路径命名。


zip(blobs)

创建一个新的 Blob 对象,该对象是包含传入 Blob 的数据的 ZIP 文件。

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 对象,该对象是包含传入 Blob 的数据的 ZIP 文件。此版本的方法允许指定文件名。

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。

已废弃的方法