在工作表或云端硬盘中使用AppScripts进行图像裁剪 [英] Image crop with AppScripts in Sheets or Drive
问题描述
我正在使用AppScripts评估Google表格作为报告生成器.要求包括插入来自Google云端硬盘的图像.输入的图像之一具有固定格式,我需要对其进行裁剪.(因此,原始图像的尺寸和裁切的尺寸是已知的.)
I am evaluating Google Sheets with AppScripts for a report generator. The requirements include inserting images from Google Drive. One of the input image has a fix format, which I need to crop. ( So the dimensions of the original image and the dimensions of the crop is known.)
我正在寻找解决方案以实现这一目标,但我一直在努力.我研究了以下方法:
I am looking for a solution to achieve this outcome, but I am struggling. I looked into the following methods:
- 云端硬盘API中没有图像裁剪功能
- 工作表 OverGridImage的图像裁切(仅调整大小)对象
-
= IMAGE()
函数没有图像裁剪 - 您可以手动创建图形,添加图像并对其进行裁剪:
- 但是您无法通过AppScripts完成此操作
- 您不能作为OverGridImage对象访问图形中的图像.(这将允许您替换它的图像.)
- 是否可以通过AppScript将幻灯片嵌入/链接到工作表中?
还有其他解决方法吗?(即使这意味着要在驱动器中创建一个新文件?)
Is there any other workaround? ( Even if it means creating a new file in drive?)
显然,有一种解决方案可以调用外部API或使用GCP服务(例如CloudFunctions)(这不是免费的).但是,我希望有一个AppScript解决方案.(或使用兼容的JS库.)
Obviously there is the solution to call an external API, or to use GCP services such as CloudFunctions ( which are not free). However I am hoping for an AppScript solution. ( Or using a compatible JS library.)
推荐答案
我相信您的目标如下.
- 您要使用Google Apps脚本来裁剪图像.
不幸的是,在当前阶段,尚无使用Google Apps脚本直接裁剪图像的方法.您的评论已经提到了这一点.因此,在这个答案中,我想提出一种使用Google Apps脚本裁剪图像的解决方法.
Unfortunately, in the current stage, there are no methods for directly cropping image using Google Apps Script. This has already been mentioned by your comment. So in this answer, I would like to propose a workaround for cropping image using Google Apps Script.
在此替代方法中,我使用Microsoft Powerpoint和Google幻灯片.此解决方法的流程如下.
In this workaround, I use Microsoft Powerpoint and Google Slides. The flow of this workaround is as follows.
- 通过设置页面大小来创建Microsoft Powerpoint(PPTX数据).
- 在这种情况下,将设置裁切留下的图像的宽度和高度.这用作图像窗口.
- 解压缩转换后的PPTX数据后,可以将其分析为XML数据.幸运的是,在Microsoft Docs,详细规范以Open XML的形式发布.因此,在这种情况下,可以使用Google Apps脚本的XmlService分析Microsoft文档(例如XLSX,DOCX和PPTX).在这种解决方法中,PPTX数据是通过包含页面大小直接创建的.我认为这种方法在其他情况下也很有用.
- 当"presentations.create"的方法被执行时.可以正确使用Slides API,这不是必需的.参考
- 那时,顶级"作物的产量很高.和左"从坐标原点减去.因为Google幻灯片的坐标原点是左上角.
- At that time, the crop of "top" and "left" is subtracted from the origin of coordinates. Because the origin of coordinates of Google Slides is the upper-left corner.
通过以上流程,可以将裁剪的图像检索为斑点.
By above flow, the cropped image can be retrieved as a blob.
在此示例脚本中,使用Google Apps脚本中的参数裁剪图像.在这种情况下,反映上述流程的脚本会有点复杂.因此,在这里,我想介绍一个使用Google Apps脚本库的示例脚本.参考当然,您可以在此处看到整个脚本.
In this sample script, an image is cropped using the parameters using Google Apps Script. In this case, the script reflecting above flow is a bit complicated. So here, I would like to introduce the sample script using a Google Apps Script library. Ref Of course, you can see the whole script there.
在使用此脚本之前,请安装Google Apps脚本库的ImgApp.参考在这种情况下,
Before you use this script, please install ImgApp of the Google Apps Script library. Ref And in this case, please enable Drive API and Slides API at Advanced Google services.
function myFunction() {
const id = "###"; // If you want to crop the image on Google Drive, please set the file ID of the image here.
const object = {blob: DriveApp.getFileById(id).getBlob(), crop: {t: 50, b: 100, l: 200, r: 100}};
const blob = ImgApp.editImage(object);
DriveApp.createFile(blob);
}
- 此示例脚本在Google云端硬盘上裁剪图像,并将裁剪后的图像创建到根文件夹中.
-
crop:{t:50,b:100,l:200,r:100}
表示t
,b
,l
和r
分别是顶部,底部,左侧和右侧.单位是像素. - This sample script crops an image on Google Drive and the cropped image is created to the root folder.
crop: {t: 50, b: 100, l: 200, r: 100}
means that the crop size oft
,b
,l
andr
are the top, bottom, left and right, respectively. The unit is pixel.
这篇关于在工作表或云端硬盘中使用AppScripts进行图像裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-