将gdoc转换成图片

编程入门 行业动态 更新时间:2024-10-26 00:30:18
本文介绍了将gdoc转换成图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以从Google文档创建图像(例如png)? 我的意思是图像,而不仅仅是pdf. GetAS仅创建pdf,但如果contentType设置为image/png或其他等效格式,则返回错误. 我的代码(实际上是微不足道的)是

is there a way to create an image (e.g. a png) from a google document? I really mean an image, not just a pdf. GetAS only creates pdf, but returns an error if contentType is set to image/png or other equivalent formats. My (actually trivial) code is

function convertFile() { var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0"; var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ"; var source = DriveApp.getFileById(SOURCE_TEMPLATE); var targetFolder = DriveApp.getFolderById(TARGET_FOLDER); var target = source.makeCopy(source,targetFolder); var newFile = DriveApp.createFile(target.getAs('image/png')); }

运行此代码时,出现以下错误(我的翻译):

When I run this code, I get the following error (my translation):

不支持从application/vnd.google-apps.document到image/png的转换.

The conversion from application/vnd.google-apps.document to image/png is not supported.

Ty

推荐答案

此答案如何?

makeCopy()返回File对象. getAs()不能用于此目的.这样,就会发生错误.

makeCopy() returns File object. getAs() cannot be used for this. By this, the error occurs.

不幸的是,在当前阶段,Google文档不能直接导出为PNG图像.因此,需要考虑解决方法. Google文档可以转换为PDF.此答案使用此.解决方法是,我建议使用一个 ConvertAPI 的外部API.我认为使用外部API可使脚本变得简单. API的这种方法(PDF到PNG API)可以从PDF数据转换为PNG数据.

Unfortunately, in the current stage, Google Document cannot be directly exported as PNG images. So it is required to think of workarounds. Google Document can be converted to PDF. This answer uses this. As a workaround, I would like to propose to use an external API which is ConvertAPI. I thought that using the external API, the script becomes simple. This a method (PDF to PNG API) of API can be converted from PDF data to PNG data.

例如,当您尝试这样做时,您也可以使用免费软件包"进行测试.当您尝试使用免费软件包"时,请在免费软件包"处注册并获取您的密钥.

When you try this, for example, you can also test this using "Free Package". When you try using "Free Package", please Sign Up at "Free Package" and retrieve your Secret key.

在运行此脚本之前,请检索您的秘密密钥并进行设置.

Before you run this script, please retrieve your Secret key and set it.

var secretkey = "###"; // Please set your secret key. var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0"; var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ"; var url = "v2.convertapi/convert/pdf/to/png?Secret=" + secretkey; var options = { method: "post", payload: {File: DriveApp.getFileById(SOURCE_TEMPLATE).getBlob()}, } var res = UrlFetchApp.fetch(url, options); res = JSON.parse(res.getContentText()); res.Files.forEach(function(e) { var blob = Utilities.newBlob(Utilities.base64Decode(e.FileData), "image/png", e.FileName); DriveApp.getFolderById(TARGET_FOLDER).createFile(blob); });

参考文献:

  • makeCopy()
  • getAs()
  • ConvertAPI
  • 将PDF转换为ConvertAPI的PNG API
  • References:

    • makeCopy()
    • getAs()
    • ConvertAPI
    • PDF to PNG API of ConvertAPI

更多推荐

将gdoc转换成图片

本文发布于:2023-06-10 02:00:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/606240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换成   图片   gdoc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!