当前位置: 首页 > 知识库问答 >
问题:

如何使用vb.net?在现有谷歌电子表格中插入新工作表

谭翔
2023-03-14

我设法使用以下代码在谷歌驱动器上创建电子表格。现在我想在创建的电子表格中添加新表。

当我创建新的电子表格时,它包含名为Sheet1的默认工作表,但我想重命名该工作表或在电子表格中插入多个工作表

Public Function Create(ByVal FileName As String) As String
        Try

            Dim service = GetGoogleAPPDriveService()


            Dim fileMetadata = New Google.Apis.Drive.v3.Data.File()
            fileMetadata.Name = FileName
            fileMetadata.MimeType = "application/vnd.google-apps.spreadsheet"

            Dim request As FilesResource.CreateRequest = service.Files.Create(fileMetadata)

            request.SupportsTeamDrives = True
            request.Fields = "id"
            System.Net.ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf AcceptAllcertification)
            Dim file = request.Execute

            Return file.Id

        Catch ex As Exception
            Throw ex
        End Try
    End Function 

Private Function GetGoogleAPPDriveService() As DriveService
        Try
            Dim Scopes1 As String() = {DriveService.Scope.Drive, DriveService.Scope.DriveFile}
            Dim ClientId As String = ""
            Dim ClientSecret As String = ""
            Dim UserCredentials As UserCredential

            Dim stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
            ClientId = GoogleClientSecrets.Load(stream).Secrets.ClientId

            Dim stream1 = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
            ClientSecret = GoogleClientSecrets.Load(stream1).Secrets.ClientSecret

            Dim cs = New ClientSecrets()
            cs.ClientId = ClientId
            cs.ClientSecret = ClientSecret

            UserCredentials = GoogleWebAuthorizationBroker.AuthorizeAsync(cs, Scopes1, Environment.UserName,
                                                                         CancellationToken.None, New FileDataStore("MyAppsToken", True)).Result


            Dim service = New DriveService(New BaseClientService.Initializer() With {
   .HttpClientInitializer = UserCredentials,
   .ApplicationName = ApplicationName})

            Return service


        Catch ex As Exception
            Throw ex
        End Try
    End Function

Public Function AcceptAllcertification(ByVal sender As Object, ByVal Certification As X509Certificate, ByVal Chain As X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function

共有2个答案

孟德曜
2023-03-14

很难确定必须在中使用哪个请求。net从json文件。我成功地在现有电子表格中添加了表格。在这里,我把vb。net代码(如果需要)



Public Function Addsheet(ByVal FileId As String, ByVal sheetName As String) As String
        Try

            Dim serv = GetGoogleAPPSheetService() 

            Dim updatesheet = New BatchUpdateSpreadsheetRequest
            Dim Req_Request As Request = New Request
            Dim Req_AddSheet As New AddSheetRequest
            Dim prop As New SheetProperties

            prop.Title = sheetName

            Req_AddSheet.Properties = prop

            Req_Request.AddSheet = Req_AddSheet

            Dim IList_req As IList(Of Request)

            Dim List_req1 = New List(Of Request)

            List_req1.Add(Req_Request)

            IList_req = List_req1.ToList
            updatesheet.Requests = IList_req
            Dim req = serv.Spreadsheets.BatchUpdate(updatesheet, FileId)
            Dim response = req.Execute

        Catch ex As Exception
            Throw ex
        End Try

    End Function

Private Function GetGoogleAPPSheetService() As SheetsService
        Try
            Dim UserCredentials As UserCredential

            Using stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
                Dim credPath As String = "token.json" 'System.Environment.GetFolderPath(Environment.SpecialFolder.Personal)
                'credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json")
                UserCredentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
            End Using

            Dim service = New SheetsService(New BaseClientService.Initializer() With {
   .HttpClientInitializer = UserCredentials,
   .ApplicationName = ApplicationName})

            Return service

        Catch ex As Exception
            Throw ex
        End Try
    End Function
龚俊捷
2023-03-14

为此,您需要使用Sheets API。查看AddSheetRequest

 类似资料:
  • 可编辑链接-https://docs.google.com/spreadsheets/d/1vrzchTHVwwzc9wgFGmtc_zBsh27CEtE7KOyeNDuLwC0/edit?usp=sharing 发布至网页-https://docs.google.com/spreadsheets/d/e/2PACX-1vRaJd3YpKnemAneU47RI58m7cxQsYFdViFBxJPV

  • 我有多个谷歌电子表格与预订数据,如预订号码,客户端名称,电子邮件,预订日期等。在所有工作表中,这些列的顺序并不相同。 我想在一个“主”电子表格中更新我所有“源”表中的所有数据。这意味着,一旦添加新行或更新现有行,数据将同步到主电子表格 实现这一目标的最佳方式是什么?Javascript或者是否存在一些现有的Google Sheets插件? 示例表1:快速船预订 示例表2:机场中转预订 主表 非常感

  • 试图复制整个电子表格,但我想没有api可以这样做。 基本上,我正在尝试做以下工作: 有一个电子表格,我想对其进行小的更改。 创建一个新的电子表格,将模板中的所有表格逐个复制到新的电子表格中(电子表格复制会更有效率) 创建新的电子表格工作正常,但从电子表格复制表格不起作用。 尝试了两种方法: 角: 给出以下错误: 对飞行前请求的响应未通过访问控制检查:无“访问控制允许原点” Google Sheet

  • 我们使用Google Sheet API v4。我们希望用空数据清除整个工作表。我们不想删除行/列。 UpdateCells调用不起作用(通过API删除列):开发人员。谷歌。com 工作UpdateCells调用(所有单元格):开发人员。谷歌。com

  • 我是谷歌脚本的新手,不知道是否有人能帮我。 我有一个共享的谷歌电子表格,基本上是用新的员工信息更新行。 我希望只有当插入这些新员工行时,特定列(比如F列)上的“ABC”字符串匹配时,才会触发电子邮件。基本上,电子邮件触发器会让我们的团队知道如何设置新的员工帐户。 有人能帮我吗?我不知道如何进行字符串匹配,也不知道如何让它专门发送给固定的电子邮件收件人。我已经安装了Python、gspread和gd

  • 我正在使用谷歌工作表api创建工作表,并希望获得工作表的iframe可嵌入链接。