download [Excel VBA] Làm việc với Google Sheets từ Excel

NguyenDang95

Senior Member
Đầu năm, gửi mọi người một tệp add-in .xlam với một số chức năng như sau:
  • Chèn dữ liệu vào Google Sheets, lấy dữ liệu từ Google Sheets về Excel, xóa dữ liệu trên Google Sheets.
  • Một số chức năng linh tinh khác như tạo, xóa, đổi tên sheet trên Google Sheets, tìm kiếm và thay thế trên Google Sheets.
Video minh họa:

Do mình không có thời gian nhiều với lại lười quá nên add-in này chỉ có vài tính năng như trên, cho nên mọi người có thể tiếp tục phát triển và thêm tính năng cho add-in này.
Add-in này chỉ bao quát một phần nhỏ so với những chức năng mà Google Sheets API hỗ trợ. Để tìm hiểu thêm về Google Sheets, vui lòng tham khảo tại đây: https://developers.google.com/sheets/api/reference/rest
Code trong add-in có thể chứa lỗi, mọi người xem qua và sửa giúp mình nhé.
Xin cảm ơn!
 

Attachments

  • GoogleSheetsAPI.zip
    1.4 MB · Views: 61
Cập nhật: Gần đây mình đã quyết định viết lại toàn bộ và hoàn chỉnh thư viện Google Sheets API chứ không để tình trạng "nửa nạc nửa mỡ", rời rạc không thống nhất như bản cũ nữa. Mọi người có thể tải về sử dụng tệp đính kèm theo bài viết này.
Tài liệu: Google Sheets API | Reference

Một ví dụ về việc sử dụng Google Sheets API trong Excel:
Giả sử người dùng có tệp Google Sheets với mã Id là 15Daerao2Xtt9ByJoVZVnE_CE9LXxma1UQa9VlkDoW9Q (mã Id nằm trong url của spreadsheet trên Google Drive, vd: https://docs.google.com/spreadsheets/d/15Daerao2Xtt9ByJoVZVnE_CE9LXxma1UQa9VlkDoW9Q/edit)
1678848615332.png

Người dùng muốn nối thêm dữ liệu (Append) với hai dòng "Hello" và "Visual Basic for Applications" vào vùng Sheet1!A1.
Dựa vào thư viện nói trên, viết macro như sau:
Code:
Private Sub Append()
    Dim objGoogleOAuth2 As GoogleOAuth2
    Set objGoogleOAuth2 = GetSession
    Dim arrValues(0 To 1, 0 To 0) As Variant
    arrValues(0, 0) = "Hello"
    arrValues(1, 0) = "Visual Basic for Applications"
    Dim objValueRange As ValueRange
    Set objValueRange = New ValueRange
    With objValueRange
        .MajorDimension = "ROWS"
        .Values = arrValues
    End With
    objGoogleOAuth2.Spreadsheets.Values.ValueInputOption = "USER_ENTERED"
    objGoogleOAuth2.Spreadsheets.Values.SpreadsheetId = "15Daerao2Xtt9ByJoVZVnE_CE9LXxma1UQa9VlkDoW9Q"
    objGoogleOAuth2.Spreadsheets.Values.Range = "Sheet1!A1"
    Debug.Print objGoogleOAuth2.Spreadsheets.Values.Append(objValueRange).Updates.UpdatedRows
End Sub

Private Function GetSession() As GoogleOAuth2
    Dim objGoogleOAuth2 As GoogleOAuth2
    Set objGoogleOAuth2 = New GoogleOAuth2
    On Error Resume Next
    With objGoogleOAuth2
        .ApplicationName = "GoogleSheetsAPI"
        .ClientID = "ClientId"
        .ClientSecret = "ClientSecret"
        .Scopes = Array("https://www.googleapis.com/auth/spreadsheets")
        .AuthorizeOAuth2
    End With
    If Err.Number = 0 Then
        Set GetSession = objGoogleOAuth2
    Else: Err.Clear
    End If
End Function

Private Sub LogOut()
    Dim objGoogleOAuth2 As GoogleOAuth2
    Set objGoogleOAuth2 = New GoogleOAuth2
    On Error Resume Next
    With objGoogleOAuth2
        .ApplicationName = "GoogleSheetsAPI"
        .ClientID = "ClientId"
        .ClientSecret = "ClientSecret"
        .LogOut Refresh_Token
    End With
End Sub

Kết quả sau khi chạy macro:
1678848907114.png

Mọi người có thể tìm thấy nhiều ví dụ khác trong tệp đính kèm theo bài viết này.
 
Tiếp theo, gửi đến mọi người một món đồ chơi, giúp lấy dữ liệu từ tệp Google Sheets bất kỳ vào Excel.
1685332102194.png
 

Attachments

  • ImportFromGoogleSheets.zip
    1,016.8 KB · Views: 62
Last edited:
Back
Top