Class Utilities

实用程序

此服务提供用于字符串编码/解码、日期格式设置、JSON 操作和其他杂项任务的实用程序。

属性

属性类型说明
CharsetCharset
DigestAlgorithmDigestAlgorithm
MacAlgorithmMacAlgorithm
RsaAlgorithmRsaAlgorithm

方法

方法返回类型简介
base64Decode(encoded)Byte[]将经过 Base64 编码的字符串解码为 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 Web 安全编码字符串。
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 对象,该对象是一个 zip 文件,其中包含传入的 Blob 中的数据。
zip(blobs, name)Blob创建一个新的 Blob 对象,该对象是一个 zip 文件,其中包含传入的 Blob 中的数据。

详细文档

base64Decode(encoded)

将 base-64 编码的字符串解码为 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[] - 以字节数组形式表示的由 base-64 编码的实参表示的原始数据。


base64Decode(encoded, charset)

将 base-64 编码的字符串解码为特定字符集中的字节数组。

// 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[] - 以字节数组形式表示的由 base-64 编码的实参表示的原始数据。


base64DecodeWebSafe(encoded)

将 base-64 网络安全编码的字符串解码为 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[] - 以字节数组形式表示的、采用 base-64 网络安全编码的实参所表示的原始数据。


base64DecodeWebSafe(encoded, charset)

将 base-64 网络安全编码的字符串解码为特定字符集中的字节数组。

// 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[] - 以字节数组形式表示的、采用 base-64 网络安全编码的实参所表示的原始数据。


base64Encode(data)

根据给定的字节数组生成一个 base-64 编码的字符串。Base 64 是一种常见的编码,可被各种无法接受二进制数据的工具接受。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 - 传入数据的 base-64 编码表示形式。


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 - 输入字符串的 base-64 编码表示形式。


base64Encode(data, charset)

根据指定字符集中的给定字符串生成一个 base-64 编码的字符串。字符集是一种对字符进行编码的方式,以便可以对字符进行编码。这些数据通常以二进制格式存储,这可能与某些数据传输协议不兼容。为了使数据兼容,它们通常会编码为 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 - 采用指定 Charset 的输入字符串的 base-64 编码表示形式。


base64EncodeWebSafe(data)

根据给定的字节数组生成 base-64 网络安全编码的字符串。Base64 是一种常见的编码,可被各种无法接受二进制数据的工具接受。Base 64 Web 安全编码通常用于电子邮件、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 - 传入数据的 base-64 网络安全编码表示形式。


base64EncodeWebSafe(data)

根据给定的字符串生成一个 base-64 Web 安全编码字符串。Base 64 是一种常见的编码,可被各种无法接受二进制数据的工具接受。Base 64 Web 安全编码通常用于电子邮件、HTTP 或 XML 文档等互联网协议中。

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

参数

名称类型说明
dataString要编码的字符串。

返回

String - 输入字符串的 base-64 网络安全编码表示形式。


base64EncodeWebSafe(data, charset)

根据指定字符集中的给定字符串生成一个 base-64 web 安全编码字符串。 字符集是一种对字符进行编码的方式,以便对字符进行编码。这些操作通常以二进制格式完成,但这种格式可能与某些数据传输协议不兼容。为了使数据兼容,它们通常会编码为 base 64,这是一种常见的编码,可被各种无法接受二进制数据的工具接受。Base 64 Web 安全编码通常用于电子邮件、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 - 采用指定 Charset 的输入字符串的 base-64 网络安全编码表示形式。


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

// 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 脚本 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 脚本 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 的字节。
contentTypeString相应 blob 的内容类型 - 可以是 null

返回

Blob - 新创建的 Blob。


newBlob(data, contentType, name)

根据字节数组、内容类型和名称创建新的 Blob 对象。许多以二进制数据作为输入的 Apps 脚本 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 脚本 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 脚本 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。
contentTypeString相应 blob 的内容类型 - 可以是 null

返回

Blob - 新创建的 Blob。


newBlob(data, contentType, name)

根据字符串、内容类型和名称创建新的 Blob 对象。许多以二进制数据作为输入的 Apps 脚本 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。
contentTypeString相应 blob 的内容类型 - 可以是 null
nameStringBlob 的名称 - 可以是 null

返回

Blob - 新创建的 Blob。


parseCsv(csv)

返回 CSV 字符串的表格状二维数组表示形式。

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

// 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 标准版 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)

休眠指定毫秒数。立即将脚本休眠指定的毫秒数。允许的最大值为 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

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 对象,该对象是一个 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]);

参数

名称类型说明
blobsBlobSource[]要压缩的 blob 数组。

返回

Blob - 一个包含输入内容的新 blob(以归档形式)。


zip(blobs, name)

创建一个新的 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');

参数

名称类型说明
blobsBlobSource[]要压缩的 blob 数组。
nameString要创建的 ZIP 文件的名称。

返回

Blob - 一个包含输入内容的新 blob(以归档形式)。

已弃用的方法