【发布时间】:2021-05-15 19:05:46
【问题描述】:
我正在尝试使用批量更新方法将 CSV 文件的内容直接加载到现有的 Google 表格中。
问题是我的代码只上传最后一条记录,但据我所知,请求对象包含所有数据。
到目前为止,我能够遍历 CSV 文件并将每一行转换为 Google Sheets ValueRange 并将每个 ValueRange 添加到数据对象,以便将其应用于 Google Sheets API 所需的 RequestBody。 这一切都基于 Google 提供的 API 文档。 Google Sheets API V4 Batch Update
我扔了几个消息框只是为了看看后台发生了什么。
以下是我目前正在使用的代码,以及可以放入 csv 文件中运行的一组示例数据。
我似乎遗漏了上传请求本身的某些内容,如果有人对此有任何意见或经验,我似乎无法克服这一点。
*注意:此代码只是一个 VB .Net winform 应用程序,在表单中添加了一个按钮。
Imports System.Threading
Imports Google.Apis.Sheets.v4
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Microsoft.VisualBasic.FileIO
Imports Google.Apis.Sheets.v4.Data
Imports Data = Google.Apis.Sheets.v4.Data
Public Class Form1
Dim ClientID As String = "<GOOGLE CLIENT>.apps.googleusercontent.com"
Dim ClientSecret As String = "<CLIENT SECRET>"
Dim Scopes As String() = {SheetsService.Scope.Spreadsheets}
Dim credential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientID,
.ClientSecret = ClientSecret},
Scopes, "user",
CancellationToken.None).Result
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim r As Integer = 1
Dim oblist = New List(Of Object)
Dim sheetsService As SheetsService = New SheetsService(New BaseClientService.Initializer With {
.HttpClientInitializer = credential,
.ApplicationName = "Google-SheetsSample/0.1"
})
Dim spreadsheetId As String = "<SPREADSHEET ID>"
Dim valueInputOption As String = "RAW"
Dim valueRanges As ValueRange = New ValueRange()
Dim data As List(Of Data.ValueRange) = New List(Of Data.ValueRange)
Dim tfp As New TextFieldParser("<PATH TO CSV FILE I WANT TO UPLOAD>")
tfp.SetDelimiters(",")
tfp.TextFieldType = FieldType.Delimited
'tfp.ReadLine() ' skip header
While tfp.EndOfData = False
Dim fields = tfp.ReadFields()
Dim range2 As String = "A" & r ' range to update
valueRanges.MajorDimension = "ROWS" '"ROWS";//COLUMNS
valueRanges.Range = range2 ' apply range to ValueRnage
' My csv has 12 fields
oblist = New List(Of Object)() From {
fields(0), fields(1), fields(2), fields(3), fields(4), fields(5), fields(6), fields(7), fields(8), fields(9), fields(10), fields(11)
}
valueRanges.Values = New List(Of IList(Of Object)) From { ' add list of objects pulled from TextFieldParser to ValueRange
oblist
}
data.Add(valueRanges) ' Add valueRange to data for RequestBody
r += 1 ' Increment Range
End While
MsgBox("Count of value ranges in data object : " & data.Count)
Dim requestBody As Data.BatchUpdateValuesRequest = New Data.BatchUpdateValuesRequest()
requestBody.ValueInputOption = valueInputOption
requestBody.Data = data
MsgBox("Request body data : " & requestBody.Data.Count)
Dim request As SpreadsheetsResource.ValuesResource.BatchUpdateRequest = sheetsService.Spreadsheets.Values.BatchUpdate(requestBody, spreadsheetId)
Dim response As Data.BatchUpdateValuesResponse = request.Execute()
MsgBox("Count of Rows uploaded to google sheet : " & response.TotalUpdatedRows)
End Sub
End Class
CSV - 示例 -
第1栏,第2栏,第3栏,第4栏,第5栏,第6栏,第7栏,第8栏,第9栏,第10栏,第11栏,第12栏 ex12,ex34,ex56,ex78,ex100,ex122,ex144,ex166,ex188,ex210,ex232,ex254 ex13,ex35,ex57,ex79,ex101,ex123,ex145,ex167,ex189,ex211,ex233,ex255 ex14,ex36,ex58,ex80,ex102,ex124,ex146,ex168,ex190,ex212,ex234,ex256 ex15,ex37,ex59,ex81,ex103,ex125,ex147,ex169,ex191,ex213,ex235,ex257 ex16,ex38,ex60,ex82,ex104,ex126,ex148,ex170,ex192,ex214,ex236,ex258 ex17,ex39,ex61,ex83,ex105,ex127,ex149,ex171,ex193,ex215,ex237,ex259 ex18,ex40,ex62,ex84,ex106,ex128,ex150,ex172,ex194,ex216,ex238,ex260 ex19,ex41,ex63,ex85,ex107,ex129,ex151,ex173,ex195,ex217,ex239,ex261 ex20,ex42,ex64,ex86,ex108,ex130,ex152,ex174,ex196,ex218,ex240,ex262 ex21,ex43,ex65,ex87,ex109,ex131,ex153,ex175,ex197,ex219,ex241,ex263
【问题讨论】:
标签: vb.net google-sheets-api .net-4.7.2