如何将范围从电子表格作为图像复制到Google幻灯片? [英] How to copy a range from a spreadsheet as an image to Google Slides?

查看:60
本文介绍了如何将范围从电子表格作为图像复制到Google幻灯片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将特定Google电子表格中的一系列单元格作为图像复制到Google幻灯片上.但是我几乎找不到有用的代码.这是我想出的,但是我仍然无法将像元范围转换为image/png.

目标:将仅存储在变量中的图像插入到特定的幻灯片中!

非常感谢您的帮助.谢谢.

  function add_WSA(){//打开电子表格var ss = SpreadsheetApp.openById("insertSpreadsheetID");var range = ss.getRange("example!A1:F20");//以A1表示法//转换为png图片var image = range.getAs('image/png');//打开特定的幻灯片(Nr.3)var slide = SlidesApp.openById("mySlidesID").getSlides()[2];//插入图片slide.insertImage(image);}  

错误:TypeError:range.getAs不是一个函数在add_WSA(report:5:21)

PS:我对社区和JavaScript还是陌生的.请耐心等待.非常感谢您提供其他更精简或更有效的解决方案.谢谢.

解决方案

此答案如何?

问题和解决方法:

不幸的是,在当前阶段,范围对象不能直接转换为PNG格式.因此,在这种情况下,需要使用解决方法.在此答案中,作为解决方法,我建议使用

注意:

  • 请在启用V8的情况下使用此脚本.
  • 例如,在这种情况下,当您想要更改字体颜色时,请在每个单元格值中使用HTML代码.

参考:

I am trying to copy a range of cells of a specific Google spreadsheet as an image onto a Google slide. But I could barely find useful code. This is what I came up with, but I still cannot transfer the cell range into an image/png.

Goal: Insert the image stored just in a variable to a specific slide!

Any help is very much appreciated. Thank you.

function add_WSA(){
  //Opening the Spreadsheet
  var ss = SpreadsheetApp.openById("insertSpreadsheetID");
  var range = ss.getRange("example!A1:F20");//in A1 Notation
  //Conversion into an png image
  var image = range.getAs('image/png');
  
  //Opening the specific Slide (Nr. 3)
  var slide = SlidesApp.openById("mySlidesID").getSlides()[2]; 
  
  //Insertion of image
  slide.insertImage(image);
}

Error: TypeError: range.getAs is not a function at add_WSA(report:5:21)

PS: I am farely new to the community and to JavaScript. Please be patient. Every other help on a leaner or more efficient solution to the problem is very much appreciated. Thank you.

解决方案

How about this answer?

Issue and workaround:

Unfortunately, in the current stage, the range object cannot be directly converted to the PNG format. So in this case, it is required to use a workaround. In this answer, as the workaround, I would like to propose to use Charts Service. When Charts Service is used, the range of Spreadsheet can be converted to an image blob.

Sample script:

function add_WSA(){
  //Opening the Spreadsheet
  var ss = SpreadsheetApp.openById("insertSpreadsheetID");
  var range = ss.getRange("example!A1:F20");//in A1 Notation
  //Conversion into an png image


  // I modified below script.
  const [header, ...values] = range.getDisplayValues();
  const table = Charts.newDataTable();
  header.forEach(e => table.addColumn(Charts.ColumnType.STRING, e));
  values.forEach(e => table.addRow(e));
  const image = Charts.newTableChart().setDataTable(table.build()).setDimensions(500, 500).setOption('alternatingRowStyle', false).build().getBlob();


  //Opening the specific Slide (Nr. 3)
  var slide = SlidesApp.openById("mySlidesID").getSlides()[2]; 
  
  //Insertion of image
  slide.insertImage(image);
}

Result:

When above script is run, the following sample result can be obtained.

Note:

  • Please use this script with enabling V8.
  • In this case, for example, when you want to change the font color, please use HTML code in each cell value.

Reference:

这篇关于如何将范围从电子表格作为图像复制到Google幻灯片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆