blackmango
Senior Member
Mấy bác cho mình hỏi trường hợp này với ạ
- ở App Script trên google sheet thì mình add macro này:
function doPost(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Change "Sheet1" if your sheet name is different
var data = JSON.parse(e.postData.contents); // Parse the incoming data
// Loop through the data and append it to the Google Sheet
for (var i = 0; i < data.length; i++) {
var row = data;
sheet.appendRow(row);
}
return
ContentService.createTextOutput("Success");
}
- còn trên vba excel thì mình add code này:
Sub UploadDataToGoogleSheets()
Dim http As Object
Dim url As String
Dim sheet As Worksheet
Dim row As Range
Dim data As Variant
Dim jsonData As String
Dim i As Long, j As Long
' URL of the Google Apps Script Web App
url = "YOUR_GOOGLE_APPS_SCRIPT_URL" ' Replace with the Web App URL you copied
' Set the worksheet and range to upload
Set sheet = ThisWorkbook.Sheets("Sheet1") ' Change the sheet name if necessary
data = sheet.UsedRange.Value ' Get all the data in the used range of the sheet
' Prepare the data as JSON
jsonData = "["
' Loop through rows and columns to convert data into JSON format
For i = 1 To UBound(data, 1)
jsonData = jsonData & "["
For j = 1 To UBound(data, 2)
jsonData = jsonData & """" & data(i, j) & """"
If j < UBound(data, 2) Then
jsonData = jsonData & ","
End If
Next j
jsonData = jsonData & "]"
If i < UBound(data, 1) Then
jsonData = jsonData & ","
End If
Next i
jsonData = jsonData & "]"
' Create the HTTP object
Set http = CreateObject("MSXML2.XMLHTTP")
' Send the data to the Google Sheets via the Google Apps Script web app
http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/json"
http.Send jsonData
' Check the response
If http.Status = 200 Then
MsgBox "Data uploaded successfully!", vbInformation
Else
MsgBox "Error uploading data: " & http.Status, vbCritical
End If
End Sub
Khi chạy macro vba thì dữ liệu được đẩy lên tuy nhiên là nó không giữ nguyên định dạng của excel mà chỉ đẩy nội dung, và khi chạy lần nữa thì dữ liệu nó không dè lên các ô cũ trên google sheet mà nó tự tìm ô trống cuối bảng tính nó đẩy dữ liệu lên đó, thành ra có 2 bảng dữ liệu, và nếu chạy lần 3 thì nó thành 3 bảng
(
Bác nào chỉ giúp mình với ạ. Thankkkk
- ở App Script trên google sheet thì mình add macro này:
function doPost(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Change "Sheet1" if your sheet name is different
var data = JSON.parse(e.postData.contents); // Parse the incoming data
// Loop through the data and append it to the Google Sheet
for (var i = 0; i < data.length; i++) {
var row = data;
sheet.appendRow(row);
}
return
ContentService.createTextOutput("Success");
}
- còn trên vba excel thì mình add code này:
Sub UploadDataToGoogleSheets()
Dim http As Object
Dim url As String
Dim sheet As Worksheet
Dim row As Range
Dim data As Variant
Dim jsonData As String
Dim i As Long, j As Long
' URL of the Google Apps Script Web App
url = "YOUR_GOOGLE_APPS_SCRIPT_URL" ' Replace with the Web App URL you copied
' Set the worksheet and range to upload
Set sheet = ThisWorkbook.Sheets("Sheet1") ' Change the sheet name if necessary
data = sheet.UsedRange.Value ' Get all the data in the used range of the sheet
' Prepare the data as JSON
jsonData = "["
' Loop through rows and columns to convert data into JSON format
For i = 1 To UBound(data, 1)
jsonData = jsonData & "["
For j = 1 To UBound(data, 2)
jsonData = jsonData & """" & data(i, j) & """"
If j < UBound(data, 2) Then
jsonData = jsonData & ","
End If
Next j
jsonData = jsonData & "]"
If i < UBound(data, 1) Then
jsonData = jsonData & ","
End If
Next i
jsonData = jsonData & "]"
' Create the HTTP object
Set http = CreateObject("MSXML2.XMLHTTP")
' Send the data to the Google Sheets via the Google Apps Script web app
http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/json"
http.Send jsonData
' Check the response
If http.Status = 200 Then
MsgBox "Data uploaded successfully!", vbInformation
Else
MsgBox "Error uploading data: " & http.Status, vbCritical
End If
End Sub
Khi chạy macro vba thì dữ liệu được đẩy lên tuy nhiên là nó không giữ nguyên định dạng của excel mà chỉ đẩy nội dung, và khi chạy lần nữa thì dữ liệu nó không dè lên các ô cũ trên google sheet mà nó tự tìm ô trống cuối bảng tính nó đẩy dữ liệu lên đó, thành ra có 2 bảng dữ liệu, và nếu chạy lần 3 thì nó thành 3 bảng
(Bác nào chỉ giúp mình với ạ. Thankkkk