首页 > 解决方案 > 谷歌文档到 base64 字符串

问题描述

在此处输入图像描述

我正在使用应用程序脚本。我想获取一个 google doc 并将其转换为 base 64 字符串以发布到外部应用程序。我一直在阅读文档,但不确定是否可以这样做。

标签: google-apps-scriptbase64google-docs

解决方案


您可以将文档转换为 blob,然后使用内置实用程序对 blob 的字节进行 base64 编码。

function getDocAsBase64String(docId) {
  const doc = DocumentApp.openById(docId)
  const bytes = doc.getBlob().getBytes()
  const base64String = Utilities.base64Encode(bytes)
  const base64WebSafeString = Utilities.base64EncodeWebSafe(bytes)
}

以下是相关的 Apps 脚本文档页面:

https://developers.google.com/apps-script/reference/document/document#getBlob() https://developers.google.com/apps-script/reference/base/blob.html#getBytes() https: //developers.google.com/apps-script/reference/utilities/utilities#base64Encode(Byte)

本页解释了普通和网络安全的 base64 字符串之间的区别:

https://en.wikipedia.org/wiki/Base64#URL_applications


推荐阅读