使用其 public URL 和 .net 库修改 google 电子表格,无需授权
Modify google spreadsheet using its public URL and .net library without auth
我有一个 google 电子表格 URL 与我共享 "everyone that have link is able to edit" 权限
我需要能够将 smth 放入它的单元格中,但我不知道该怎么做,google 文档对此没有帮助。
有任何想法吗?
Google 文档共享 url 是 https://docs.google.com/spreadsheets/d/1oKSViNtjtaKjdckJBiIQEZHgbUImnmiNOCQsYpIGdl0/edit?usp=sharing
我可以通过网络浏览器对它做任何事情,所以我可能可以用 .net 库来做吗?
这对我有用:
- 在 https://console.developers.google.com 创建一个新的 google 项目并创建一个服务帐户密钥。
- 添加"Drive api"
写一些代码
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();
}
}
}
这里的技巧是获取 Sheet Id 并在查询 url 中使用它,之后 /private/full
我有一个 google 电子表格 URL 与我共享 "everyone that have link is able to edit" 权限 我需要能够将 smth 放入它的单元格中,但我不知道该怎么做,google 文档对此没有帮助。 有任何想法吗? Google 文档共享 url 是 https://docs.google.com/spreadsheets/d/1oKSViNtjtaKjdckJBiIQEZHgbUImnmiNOCQsYpIGdl0/edit?usp=sharing
我可以通过网络浏览器对它做任何事情,所以我可能可以用 .net 库来做吗?
这对我有用:
- 在 https://console.developers.google.com 创建一个新的 google 项目并创建一个服务帐户密钥。
- 添加"Drive api"
写一些代码
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(); } } }
这里的技巧是获取 Sheet Id 并在查询 url 中使用它,之后 /private/full