2021/10/27

VBA / 透過Excel VBA下載在GoogleCharts製作QrCode的圖檔

在Google Apps Script操作的Gg / 下載在GoogleCharts製作QrCode的圖檔

因為是直接從程式碼進行操作

所以不能設定文字對話框來輸入課程代碼

於是嘗試在Excel VBA來達成

1.執行程序的按鈕

2.輸入課程代碼

3.存檔用的名稱

基本原理是先透過XMLHTTP連結Google QR Code API取得回傳的圖檔資料

再透過ADODB.Stream進行資料處理並儲存成圖檔

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Public Sub downloadPic()
    '課程代碼
    courseId = InputBox("請輸入課程代碼")
    courseName = InputBox("請輸入課程名稱")
    'QR Code 路徑
    myURL = "https://chart.googleapis.com/chart?chs=120x120&cht=qr&chld=M|3&chl=https://inservice.edu.tw/NAPP/CourseView.aspx?cid=" & courseId
    
    '工作簿所在路徑
    strSavePath = ThisWorkbook.path
    
    '建立 XMLHTTP物件來連線
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    
    'WinHttpReq.Open "GET", myURL, False, "username", "password"
    WinHttpReq.Open "GET", myURL, False
    WinHttpReq.send
    
    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1                      ' 1 = 二進制 , 2=文件檔
        oStream.Write WinHttpReq.responseBody '寫入WinHttpReq.responseBody
        oStream.SaveToFile strSavePath & "\" & courseName & ".png", 2     ' 1 = 不複寫, 2 = 複寫
        oStream.Close
        MsgBox "下載成功"
    End If
End Sub

 

#2 ~ 9

設定基本參數,包含透過文字對話框輸入課程代碼,以及圖檔儲存的名稱

並且設定將圖檔儲存在工作簿所在的資料夾

#11 ~ 16

設定 XMLHTTP

#18 ~ 26

透過判斷式來判斷伺服器能正確回傳資料時才進行檔案處理

使用ADODB.Stream來處理回傳的資料

 

參考資源

Excel VBA 的眉眉角角Day29:如何抓取網路上的資料?以issuu.com為例

Stream 物件屬性、方法和事件

ADO Stream Object

Gg / 下載在GoogleCharts製作QrCode的圖檔

下載在GoogleCharts製作QrCode的圖檔

這是從Gg / 使用GoogleCharts快速製作QrCode延伸而來的

基本原理是透過fetch傳遞QR Code的參數到Google QR Code API

取得回傳的QR Code 圖檔

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function myQR() {
  //課程代碼
  var cId = '3116469';
  
  //fetch傳遞的payload資料設定
  var params={
    'chs':'120x120',
    'cht':'qr',
    'chld':'M|3',
    'chl':'https://inservice.edu.tw/NAPP/CourseView.aspx?cid='+cId
  }

  //fetch參數設定
  var options={
     'menthod':'get',
     'payload':params
  }

  //Google qr code api
  var url = 'https://chart.googleapis.com/chart' ;
  
  //fetch連線
  var hData = UrlFetchApp.fetch(url,options);
  
  //伺服器連線情況代碼
  var rowRe = hData.getResponseCode();
  Logger.log(rowRe);
  //Logger.log(hData.getContent());
  
  if (rowRe == '200'){
    //#1
    var fileBlob = hData.getBlob().getAs('image/png').setName('研習代碼-' + cId + '.png');
    fileBlob= DriveApp.createFile(fileBlob);
    
    //#2
    //var fileBlob = Utilities.newBlob(hData.getContent(), 'image/png', '研習代碼-' + cId + '.png');
    //fileBlob= DriveApp.createFile(fileBlob);
        
    Logger.log('fetch 成功')
    Logger.log(fileBlob.getDownloadUrl());

  }else{

    Logger.log('fetch 失敗');

  }

}

 

#2 – 20

是fetch相關參數設定

包含傳入的資料以及連線方式

#23

執行fetch連線

並用代數hData來接收連線的回傳資料

#30 ~ 46

透過判斷式來判斷伺服器情況,成功取的回傳資料才進行檔案處理

儲存回傳的資料方式有2種

hData.getBlob().getAs('image/png').setName('研習代碼-' + cId + '.png');
Utilities.newBlob(hData.getContent(), 'image/png', '研習代碼-' + cId + '.png');

差別在於處理Blob的方式

最後都是透過DriveApp.createFile()產生實體檔案

由於是透過Google Apps Script操作

產生的檔案沒有特別設定的話都是儲存在雲端硬碟的根目錄下