使用Google Apps脚本将图片插入云端硬盘中的Spresheet单元格 [英] Insert Image into Spresheet Cell from Drive using Google Apps Script

查看:217
本文介绍了使用Google Apps脚本将图片插入云端硬盘中的Spresheet单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过Google云端硬盘将图片文件添加到我的电子表格中。我看到有一个内置的图像函数available = image,但这需要一个URL,并且您的图像文件在互联网上公开分享,我正在使用数字资产并且无法公开分享。

I would like to be able to add an image file into my spreadsheet from Google Drive. I see there is a built-in image function available =image, but this requires a URL and that your image files are shared publicly on the internet, I am working with digital assets and can not share them publicly.

我有下面的代码,这个工作,但没有添加到所需的单元格,这是可能的吗?

I have the following code, this works but does not add to the required cell, is this at all possible?

function insertImageFromDrive(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  var fileId = '0B5UAkV0avfiKNGYtd1VzUzlsTVk';
  var img = DriveApp.getFileById(fileId).getBlob();


  sheet.insertImage(img, 4, 3)
}


推荐答案

请尝试使用指南从应用程序脚本中将图像插入电子表格

function insertImageOnSpreadsheet() {
  var SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL_HERE';
  // Name of the specific sheet in the spreadsheet.
  var SHEET_NAME = 'INSERT_SHEET_NAME_HERE';

  var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = ss.getSheetByName(SHEET_NAME);

  var response = UrlFetchApp.fetch(
      'https://developers.google.com/adwords/scripts/images/reports.png');
  var binaryData = response.getContent();

  // Insert the image in cell A1.
  var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
  sheet.insertImage(blob, 1, 1);
}

只需更换必要的值即可。您插入图片的部分位于:

Just replace the necessary values. The part where you indicate which cell you insert the image is in:

sheet.insertImage(blob, 1, 1);

这篇关于使用Google Apps脚本将图片插入云端硬盘中的Spresheet单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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