gpt4 book ai didi

c# - 在 Google Sheet c# 中创建一个新工作表

转载 作者:太空狗 更新时间:2023-10-29 21:26:31 32 4
gpt4 key购买 nike

我可以使用以下代码检索和更新 Google 表格中的值:

private void btnUpdate_Click(object sender, EventArgs e)
{
if (cbYards.Text == "Select Yard")
{
MessageBox.Show(@"Please select a yard.");
return;
}
UserCredential credential;

using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}

var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

IList<IList<Object>> list = new List<IList<Object>>() { };
for (var i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
var formula = "=IFERROR(VLOOKUP(B"+(i+2)+",Names!$A$2:$B,2,FALSE),\"No Record\")";
List<object> lists = new List<object>() { formula, dataGridView1.Rows[i].Cells[0].Value.ToString(),
dataGridView1.Rows[i].Cells[1].Value.ToString() };
list.Add(lists);
}

var range = cbYards.Text+"!A2:C";
ValueRange VRange = new ValueRange();
VRange.Range = range;
VRange.Values = list;

//ValueRange response = request.Execute();
ValueRange valueRange = new ValueRange();
valueRange.MajorDimension = "COLUMNS";

SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
upd.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
UpdateValuesResponse response = upd.Execute();
}

我的问题是如何在我当前使用的 Google 表格中创建新表格。我以为我需要做的就是更换

SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);

SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.create();

但显然这是错误的...

我不明白如何执行 Method: spreadsheets.create 文档中的说明

JSon 与我将如何使用 C# 进行编码有什么关系?非常感谢您的帮助。

更新我在这里找到了这个,但它并不完整,因为它仍然给我一个错误

Additional information: Object reference not set to an instance of an object.

这是更新后的代码:

private void button1_Click(object sender, EventArgs e)
{
UserCredential credential;

using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}

var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

string sheetName = string.Format("{0} - {1}-{2}", cbYards.Text, fromDate.Value.ToShortDateString(), toDate.Value.ToShortDateString());
var myNewSheet = new Google.Apis.Sheets.v4.Data.Spreadsheet();
myNewSheet.Properties = new SpreadsheetProperties();
myNewSheet.Properties.Title = sheetName;
var newSheet = service.Spreadsheets.Create(myNewSheet).Execute();
}

最佳答案

客户端库中有一个创建方法,您只需将新的工作表对象传递给它即可。不要忘记在分配标题之前实例化属性(下面的第 2 行)

var myNewSheet = new Google.Apis.Sheets.v4.Data.Spreadsheet();
myNewSheet.Properties = new SpreadsheetProperties();
myNewSheet.Properties.Title = "Daimtos awsom sheet";
var awsomNewSheet= service.Spreadsheets.Create(myNewSheet).Execute();

注意:我同意你的观点,文档页面是无用的。

关于c# - 在 Google Sheet c# 中创建一个新工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439266/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com