Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

How do I insert new sheet in Existing google spreadsheet using vb.net?

I am manage to create spreadsheet on google drive using following code. Now I want to add new sheets in that created spreadsheet.

When I Create new spreadsheet , it contains default sheet having name Sheet1 but I want to rename that sheet or insert more than one sheets in Spreadsheet

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

question from:https://stackoverflow.com/questions/65917076/how-do-i-insert-new-sheet-in-existing-google-spreadsheet-using-vb-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...