如何从电子表格中获取"image in cell"并将其作为INLINE_IMAGE复制到文档中?



>我已经设置了一个电子表格作为数据库来存储类似目录的信息。此信息的一个重要元素是最终用户使用 Google 电子表格的菜单选项"在单元格中插入 --> 图像 -->图像"插入用户正面工作表中的图像。然后,在脚本中使用"SpreadsheetApp.CopyPasteType.PASTE_VALUES"将此图像复制到存储表中。

现在,我正在制作一个文档作为存储在数据库中的信息的输出。问题是我无法将图像从电子表格解析到文档。我什至无法从电子表格中获取它。

我在网上冲浪寻找解决方案,但一无所获。我发现的最接近的是一个 2 年前没有直接回答的类似问题:如何将 IMAGE 从谷歌电子表格复制到脚本中的谷歌文档?

我的逻辑是,如果我将单元格中的图像粘贴为值,我应该能够使用 .getValue() 将其取回,但显然情况并非如此。我试图把它作为一个斑点,但没有运气。我对斑点不太熟悉,所以可能我做错了。

任何想法将不胜感激:)

// This is the code I used to put the image in the cell (THIS WORKS, JUST FOR CONTEXT)
var fotosToCopy = cotizador.getRangeByName('CotizacionFotos');
fotosToCopy.copyTo(destino.getRange(fila,2), 
SpreadsheetApp.CopyPasteType.PASTE_VALUES, true);
// This is the code I'm trying to get the image from the cell (NOT WORKING)
var fotosToCopy = origen.getRange(personajeRow,2).getValue(); //I've tried .getFormula() with no results; origen is the search range, personajeRow is the row to do the search and both work.
// This is what I'm using to put the image in the document
var doc = DocumentApp.openById(cotizacionId); // cotizacionId is working
var body = doc.getBody();
var foto = body.getImages(); // I'll replace the only existing image
var parent = foto[0].getParent();
parent.insertInlineImage(parent.getChildIndex(foto)+1, fotosToCopy[0]); //fotosToCopy[0] is the issue, it returns "undefined"
foto[0].removeFromParent();

理想情况下,文档中现有的内联图像应替换为电子表格中的单元格图像。我得到的错误是:"执行失败:无法将数组转换为元素"。

知道吗?

所以我没有很干净地编码,但它应该给你一些接近你想要的东西,但首先是一个解释:

Google 表格无法返回图像的blob,只能返回对图像的引用,但 Google 云端硬盘可以。

我下面的解决方案采用Google云端硬盘文件夹中第一张图像的blob,并根据我提供给函数的DocID将其添加到文档中:

function retriveFolder(folderName,imageName,docID){
var imageFolder = DriveApp.getFoldersByName(folderName).next();
var firstImageinFolder = imageFolder.getFilesByName(imageName).next();
var imageBlob =  firstImageinFolder.getBlob();
DocumentApp.openById(docID).getBody().insertImage(1, imageBlob);
}

我希望这有所帮助,当我遇到同样的问题时,我能想到的最好的办法。不理想,但应该是一个合适的解决方法。

最新更新