如何使用.net google sheet API在Google电子表格中设置“发布到网络”?

时间:2016-10-19 15:24:55

标签: .net google-sheets google-spreadsheet-api google-sheets-api

我正在使用google sheet api创建工作表,并希望获得工作表的iframe嵌入式链接。

ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                {
                    Scopes = new[] { SheetsService.Scope.Spreadsheets,DriveService.Scope.DriveFile}
                }.FromCertificate(certificate));
            BaseClientService.Initializer clientService = new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            };
            var service = new SheetsService(clientService);
            var spareSheet = new Spreadsheet();
            spareSheet.Properties = new SpreadsheetProperties();
            spareSheet.Properties.Title = "TestSheet";

            spareSheet = service.Spreadsheets.Create(spareSheet).Execute();  

1 个答案:

答案 0 :(得分:1)

 var driveService = new DriveService(clientService);


            var fileMetadata = new Google.Apis.Drive.v3.Data.File();
            fileMetadata.Name = fileName;
            fileMetadata.MimeType = "application/vnd.google-apps.spreadsheet";
            var fileRequest = driveService.Files.Create(fileMetadata);
            fileRequest.Fields = "id";
            var file = fileRequest.Execute();
            Console.WriteLine("File ID: " + file.Id);
           // Console.WriteLine("URL"+file.)
            Permission userPermission = new Permission();
            userPermission.Type = "anyone";
            userPermission.Role = "writer";
            //   userPermission.we
            var revision = new Revision();
            revision.PublishAuto = true;
            revision.Published = true;
            revision.PublishedOutsideDomain = true;
            driveService.Permissions.Create(userPermission, file.Id).Execute();
            var request = driveService.Revisions.Update(revision, file.Id, "head");
            request.Execute();
string iframeURl="<iframe src='https://docs.google.com/spreadsheets/d/"+file.Id+"/edit?rm=minimal#gid=1333667591' height='900' width='800'></iframe>";