使用Google脚本将Google电子表格的范围转换为图像 [英] Get range of google spreadsheet into an image using google script
问题描述
我正在构建一种搜索工具,通过该工具可以使用Google脚本将Google范围转换为图像.我试图将数据范围粘贴到Google幻灯片,然后从那里获取缩略图及其网址.我正在搜索其他任何直接为我提供Google工作表所选范围的图片网址的工具.
I'm building a search tool by which one can convert a google range into an image using google script. I tried to paste that data range to google slides and from there I get thumbnails and their url. I'm searching any other tools that directly give me the url of image of the selected range of a google sheet.
谢谢
推荐答案
这是一个非常有趣的问题.
This is a very interesting question.
我不确定这样做的原因,但尽管如此,这是一个答案:
I am unsure of the reasoning behind doing this, but nonetheless, here is an answer:
这会在工作表顶部创建一个自定义菜单,显示:
This creates a custom menu on the top of your sheet that says:
自定义功能 => 将范围导出到图像文件.
当您单击它时:
- 将您选择的所有内容转换为工作表中的表格
- 保存
- 从中生成图像
- 将图像保存到驱动器中
- 显示一个弹出窗口,其中包含已保存图像的链接.
该代码已准备好处理要导出的多个范围,但是现在它仅导出选定的范围.
The code is ready to handle multiple ranges being exported, but right now it exports only the selected range.
function onOpen(e) {
//Create custom menu to export range to Slides.
SpreadsheetApp.getUi()
.createMenu('Custom Functions')
.addItem('Export Range to Image Files', 'SelectedRangeToImage')
.addToUi();
}
function SelectedRangeToImage() {
var slide = RangeToSlides();
var slideId = slide.getId();
var images = [];
for (var x=0; x<slide.getSlides().length;x++) {
var image = SlidesToImage(slide.getName()+x, slideId, slide.getSlides()[x].getObjectId());
images.push(image);
}
//Show interface with links to all images
var ui = SpreadsheetApp.getUi();
var html = HtmlService.createHtmlOutput();
html.append("<p>Your images:</p>");
html.append("<ul>");
for (var i=0; i<images.length; i++) {
html.append("<li><a href='"+images[i].getUrl()+"'>"+images[i].getName()+"</a></li>");
}
html.append("</ul>");
html.append("<input type='button' value='Close' onclick='google.script.host.close()' />");
ui.showModalDialog(html, "Exporting results:");
}
function RangeToSlides() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var rangeValues = range.getDisplayValues();
var rangeHorizontalAlignments = range.getHorizontalAlignments()
var rangeBackgrounds = range.getBackgrounds();
var rangeFontWeights = range.getFontWeights();
var sl = SlidesApp.create("ExportToImage"+new Date());
var slide = sl.getSlides()[0];
//Create table with size of the range
var table = slide.insertTable(rangeValues.length, rangeValues[0].length);
for (var x=0; x<rangeValues.length; x++) {
for (var y=0; y<rangeValues[x].length; y++) {
var cell = table.getCell(x,y);
cell.getText().setText(rangeValues[x][y]); //Set text
cell.getFill().setSolidFill(rangeBackgrounds[x][y]); //Set background
cell.getText().getTextStyle().setBold(rangeFontWeights[x][y]=="bold"?true:false); //Set text formatting
var alignment;
switch(rangeHorizontalAlignments[x][y]) {
case "general-left":
alignment = SlidesApp.ParagraphAlignment.START;
break;
case "general-right":
alignment = SlidesApp.ParagraphAlignment.END;
break;
case "center":
alignment = SlidesApp.ParagraphAlignment.CENTER;
break;
}
cell.getText().getParagraphStyle().setParagraphAlignment(alignment); //Set text alignment
}
}
sl.saveAndClose();
return sl;
}
function SlidesToImage(name, presentationId, slideId) {
var url = "https://slides.googleapis.com/v1/presentations/"+presentationId+"/pages/"+slideId+"/thumbnail";
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var responseJson = JSON.parse(response.getContentText());
var imageurl = responseJson.contentUrl;
var imageResponse = UrlFetchApp.fetch(imageurl, options);
var blob = imageResponse.getBlob();
blob.setName(name);
var resultingFile = DriveApp.createFile(blob);
return resultingFile;
}
希望这会有所帮助!
参考:
- https://stackoverflow.com/a/51391196/11869748
- 如何将Google幻灯片下载为图像?
- https://developers.google.com/slides /reference/rest/v1/presentations.pages/getThumbnail
- https://stackoverflow.com/a/51391196/11869748
- How to download Google Slides as images?
- https://developers.google.com/slides/reference/rest/v1/presentations.pages/getThumbnail
这篇关于使用Google脚本将Google电子表格的范围转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!