使用其公共URL和.net库修改Google电子表格,而无需身份验证
本文关键字:电子表格 Google 身份验证 修改 URL net | 更新日期: 2023-09-27 18:31:33
我有一个谷歌电子表格网址,与我共享了"每个有链接的人都可以编辑"权限我需要能够将smth放入它的单元格中,但我不知道该怎么做,谷歌文档对此没有帮助。有什么想法吗?谷歌文档共享网址 https://docs.google.com/spreadsheets/d/1oKSViNtjtaKjdckJBiIQEZHgbUImnmiNOCQsYpIGdl0/edit?usp=sharing
我可以通过网络浏览器用它做任何事情,所以我可能可以用 .net 库来做?
这是对我有用的:
- 在 https://console.developers.google.com 创建新的 Google 项目并创建一个服务帐号密钥。
- 添加"云端硬盘接口"
-
编写一些代码
static void DoSheet() { var certificate = new X509Certificate2(@"cert.p12", "notasecret", X509KeyStorageFlags.Exportable); const string scope = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds"; var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer("project email") { Scopes = new[] { scope } }.FromCertificate(certificate)); var requestFactory = new GDataRequestFactory("App name"); bool success = credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result; requestFactory.CustomHeaders.Add($"Authorization: Bearer {credential.Token.AccessToken}"); var service = new SpreadsheetsService("myproject-id") {RequestFactory = requestFactory}; var path = $"https://spreadsheets.google.com/feeds/worksheets/1oKSViNtjtaKjdckJBiIQEZHgbUImnmiNOCQsYpIGdl0/private/full"; var query = new WorksheetQuery(path); var feed = service.Query(query); var worksheet = (WorksheetEntry)feed.Entries[0]; var cellQuery = new CellQuery(worksheet.CellFeedLink); var cellFeed = service.Query(cellQuery); // Iterate through each cell, printing its value. foreach (var atomEntry in cellFeed.Entries) { var cell = (CellEntry) atomEntry; if (cell.Title.Text == "A1") { cell.InputValue = "200"; cell.Update(); } else if (cell.Title.Text == "B1") { cell.InputValue = "=SUM(A1, 200)"; cell.Update(); } } }
这里的诀窍是获取工作表 ID 并在查询 url 中使用它/private/full