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要解码的数据字符串。
charsetCharsetCharset,用于指定输入的字符集。

返程

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要解码的网络安全数据字符串。
charsetCharsetCharset,用于指定输入的字符集。

返程

Byte[] - 以字节数组形式表示的 base-64 网络安全编码参数表示的原始数据。


base64Encode(data)

根据给定的字节数组生成 base-64 编码的字符串。Base64 编码被各种工具所接受的常见编码。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[]要编码的数据的 byte[]。

返程

String - 传入的数据的 base-64 编码表示形式。


base64Encode(data)

根据给定字符串生成 base-64 编码的字符串。Base64 编码被各种工具接受的常见编码,它们无法接受二进制数据。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 编码的字符串。字符集是一种字符编码,可对字符进行编码。这些数据通常以二进制格式完成,这通常与某些数据传输协议不兼容。为了使数据兼容,它们通常会被编码为 base64 编码。这是一种 60 种通用编码,无法接受二进制数据。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要编码的数据字符串。
charsetCharsetCharset,用于指定输入的字符集。

返程

String - 具有给定 Charset 的输入字符串的 base-64 编码表示形式。


base64EncodeWebSafe(data)

根据给定的字节数组生成 base-64 网络安全编码字符串。Base64 编码被各种工具接受的常见编码,它们无法接受二进制数据。Base64 网络安全协议常用于互联网协议(如电子邮件、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 网络安全编码字符串。Base64 编码被各种工具所接受的常见编码。Base64 网络安全协议常用于互联网协议(如电子邮件、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 网络安全编码字符串。 字符集是一种字符编码,可对字符进行编码。这些测试通常以二进制格式完成,这通常与特定数据传输协议不兼容。为了使数据兼容,它们通常会被编码为 base64 编码。64 种编码是各种不接受二进制数据的工具的常见编码。Base64 网络安全协议常用于互联网协议(如电子邮件、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要编码的数据字符串。
charsetCharsetCharset,用于指定输入的字符集。

返程

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


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 脚本 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 脚本 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 的字节数。
contentTypeString- blob 的内容类型 - 可以是 null
nameStringblob 的名称 - 可以是 null

返程

Blob - 新创建的 Blob。


newBlob(data)

根据字符串创建一个新的 Blob 对象。Blob 在许多将二进制数据作为输入的 Apps 脚本 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 脚本 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 字符串的表格 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 中的完整路径。


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 作为归档。

已废弃的方法