Google Sheets API v4 C#更新单元格 [英] Google Sheets API v4 C# update a cell

查看:172
本文介绍了Google Sheets API v4 C#更新单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有开发人员网站中的获取单元格值c#示例,使用Google表格API v4。我试图修改这个例子来更新一个值为Tom的单元格。我被困在SpreadSheets.Values.Update的设置中。

 使用Google.Apis.Auth.OAuth2; 
使用Google.Apis.Sheets.v4;
使用Google.Apis.Sheets.v4.Data;
使用Google.Apis.Services;
使用Google.Apis.Util.Store;
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading;
使用System.Threading.Tasks;

命名空间GoogleSheetsAPI4_v1console
{
class程序
{
//如果修改这些范围,删除以前保存的凭证
// at〜 /.credentials/sheets.googleapis.com-dotnet-quickstart.json
static string [] Scopes = {SheetsService.Scope.Spreadsheet};
static string ApplicationName =TestSpreadsheet;

static void Main(string [] args)
{
UserCredential credential;

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

凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(流).Secrets,
作用域,
用户,
CancellationToken.None,
新的FileDataStore(credPath,true))。
Console.WriteLine(凭证文件保存到:+ credPath);
}

//创建Google Sheets API服务。
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName = ApplicationName,
});


//定义请求参数。
String spreadsheetId =<< myspreadsheetid>>;
String range =Sheet1!D5; //单元格D5
String myNewCellValue =Tom;
SpreadsheetsResource.ValuesResource.UpdateRequest request = service.Spreadsheets.Values.Update(<<<这里是什么?>>,spreadsheetId,range);

//在示例电子表格中打印学生的姓名和专业:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
ValueRange response = request.Execute();
IList< IList< Object>> values = response.Values;
Console.WriteLine(values);

$ b}
}
}


解决方案

这是一个使用控制台编写单元格的工作示例。谢谢大家谁协助!!

 使用Google.Apis.Auth.OAuth2; 
使用Google.Apis.Sheets.v4;
使用Google.Apis.Sheets.v4.Data;
使用Google.Apis.Services;
使用Google.Apis.Util.Store;
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading;
使用System.Threading.Tasks;

命名空间GoogleSheetsAPI4_v1console
{
class程序
{
//如果修改这些范围,删除以前保存的凭证
// at〜 /.credentials/sheets.googleapis.com-dotnet-quickstart.json
static string [] Scopes = {SheetsService.Scope.Spreadsheets}; // static string [] Scopes = {SheetsService.Scope.SpreadsheetsReadonly};
static string ApplicationName =< MYSpreadsheet>;

static void Main(string [] args)
{
UserCredential credential;

使用(var stream =
new FileStream(client_secret.json,FileMode.Open,FileAccess.Read))
{
字符串credPath = System.Environment .GetFolderPath(
System.Environment.SpecialFolder.Personal);


凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
作用域,
用户,
CancellationToken .None,
new FileDataStore(credPath,true))。
Console.WriteLine(凭证文件保存到:+ credPath);
}

//创建Google Sheets API服务。
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName = ApplicationName,
});



String spreadsheetId2 =<我的电子表格ID>;
String range2 =<我的页面名称>!F5; //更新单元格F5
ValueRange valueRange = new ValueRange();
valueRange.MajorDimension =COLUMNS; //ROWS; // COLUMNS

var oblist = new List< object>(){My Cell Text};
valueRange.Values = new List< IList< object>> {oblist};

SpreadsheetsResource.ValuesResource.UpdateRequest update = service.Spreadsheets.Values.Update(valueRange,spreadsheetId2,range2);
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
UpdateValuesResponse result2 = update.Execute();

Console.WriteLine(done!);




code $ pre

Does anyone have a good C# example for updating a cell with the v4 API?

I have the get cell values c# example from the developer website working with Google Sheets API v4. I am trying to modify the example to update a cell with a value of "Tom". I'm stuck on the settings for SpreadSheets.Values.Update.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GoogleSheetsAPI4_v1console
    {
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { SheetsService.Scope.Spreadsheet };
        static string ApplicationName = "TestSpreadsheet";

        static void Main(string[] args)
        {
            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);
            }

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });


            // Define request parameters.
            String spreadsheetId = "<<myspreadsheetid>>";
            String range = "Sheet1!D5";  // single cell D5
            String myNewCellValue = "Tom";
            SpreadsheetsResource.ValuesResource.UpdateRequest request = service.Spreadsheets.Values.Update(<<what goes here?>>, spreadsheetId, range);

            // Prints the names and majors of students in a sample spreadsheet:
            // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
            ValueRange response = request.Execute();
            IList<IList<Object>> values = response.Values;
            Console.WriteLine(values);


        }
    }
}

解决方案

Here is a working example using a console to write a singe cell. Thank you everyone who assisted !!

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GoogleSheetsAPI4_v1console
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { SheetsService.Scope.Spreadsheets}; // static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
        static string ApplicationName = "<MYSpreadsheet>";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);


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

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });



            String spreadsheetId2 = "<my spreadsheet ID>";
            String range2 = "<my page name>!F5";  // update cell F5 
            ValueRange valueRange = new ValueRange();
            valueRange.MajorDimension = "COLUMNS";//"ROWS";//COLUMNS

            var oblist = new List<object>() { "My Cell Text" };
            valueRange.Values = new List<IList<object>> { oblist };

            SpreadsheetsResource.ValuesResource.UpdateRequest update = service.Spreadsheets.Values.Update(valueRange, spreadsheetId2, range2);
            update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
            UpdateValuesResponse result2 = update.Execute();

            Console.WriteLine("done!");

        }
    }
}

这篇关于Google Sheets API v4 C#更新单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆