Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2019年1月27日日曜日

Googleドライブ内の画像ファイルをbase64にencodeしたい


Googleドライブ内の画像ファイル
https://drive.google.com/file/d/ID/view?usp=sharing



data:image/png;base64, ここにbase64変換した文字列

の形にして返したい



コード.gs
function getBase64Data() {
  var url = "https://drive.google.com/file/d/ID/view?usp=sharing";
  var id = url.split("/d/")[1].split("/")[0];
  var file = DriveApp.getFileById(id);
  var blob = file.getBlob();
  var content_type = blob.getContentType();
  var base64 = Utilities.base64Encode(blob.getBytes());
  var data = "data:" + content_type + ";base64, " + base64;
  return data;
}



参考

base64Encode(data)
https://developers.google.com/apps-script/reference/utilities/utilities#base64Encode(Byte)

Latest post

スプレッドシートの空白セルを直前の値で埋めたい

A列の空白セルに直前の値を入れたくて書いたコードです スプレッドシートに以下のようなBeforeの表があるとき (A列に空白セルがある) Before 1 A B 2 エリア 都市 3 東京 新宿 4 渋谷 5 池袋 6 神奈川 横浜 7 川崎 8 相模原 9 千葉 千葉 10 ...