彰化一整天的論壇

 找回密碼
 立即註冊
查看: 134|回復: 0

【轉貼】將google表單的圖片傳送到LineNotify

[複製鏈接]
發表於 2020-5-11 09:24:49 | 顯示全部樓層 |閱讀模式
  1. //程式碼開始

  2. function getFormData(e) {
  3.   var token = ""; //Line Notify 權杖

  4.   var form = FormApp.getActiveForm();
  5.   var formTitle = form.getTitle();
  6.   var currentItemResponses = e.response.getItemResponses();
  7.   var boundary = "Boris Lu @ http://www.youtube.com/c/borispcp";
  8.   var message = "\n\n" + formTitle;
  9.   for (var i = 0; i < currentItemResponses.length - 1; i++) {
  10.     if (currentItemResponses[i].getResponse() != "") { message += "\n\n" + currentItemResponses[i].getItem().getTitle() + ":" + currentItemResponses[i].getResponse();}
  11.   }
  12.   var imageLocation = currentItemResponses[currentItemResponses.length - 1].getResponse();
  13.   try {
  14.     var blob = DriveApp.getFileById(imageLocation).getBlob();
  15.     var imageData = Utilities.newBlob(
  16.       "--" + boundary + "\r\n"
  17.       + "Content-Disposition: form-data; name=\"message\"; \r\n\r\n" + message + "\r\n"
  18.       + "--" + boundary + "\r\n"
  19.       + "Content-Disposition: form-data; name=\"imageFile\"; filename=\"" + blob.getName() + "\"\r\n"
  20.       + "Content-Type: " + blob.getContentType() + "\r\n\r\n"
  21.     ).getBytes();
  22.     imageData = imageData.concat(blob.getBytes());
  23.     imageData = imageData.concat(Utilities.newBlob("\r\n--" + boundary + "--\r\n").getBytes());
  24.     sendImagetoLineNotify(imageData, token, boundary);
  25.   }
  26.   catch(err) {
  27.     message += "\n\n" + currentItemResponses[i].getItem().getTitle() + ":" + currentItemResponses[i].getResponse();
  28.     sendLineNotify(message, token);
  29.   }
  30. }

  31. function sendImagetoLineNotify(imageData, token, boundary){
  32.   var options = {
  33.     "method" : "post",
  34.     "contentType" : "multipart/form-data; boundary=" + boundary,
  35.     "payload" : imageData,    
  36.     "headers" : {"Authorization" : "Bearer " + token}
  37.   };
  38.   UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
  39. }

  40. function sendLineNotify(message, token){
  41.   var options =
  42.    {
  43.      "method"  : "post",
  44.      "payload" : {"message" : message},
  45.      "headers" : {"Authorization" : "Bearer " + token}
  46.    };
  47.    UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
  48. }

  49. //程式碼結束
複製代碼
程式碼來源:
https://www.youtube.com/watch?v=d2PBPFVMMXE
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)  

GMT+8, 2020-5-31 06:36 , Processed in 0.110091 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表