Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

c# adding list<string> to each cell in column google sheets api

I have a list of string with data. My mission is simple: need to write data from list to rows starts with A2 cell.

I found the code on internet, but the code only allows to write one value to a one cell. I need to write a list of strings starting from cell A2.

Please help me.

This code solved my problem:

 public void AddRow(int length, List<string> headers, List<string> prices)
    {
        // Setting Cell Name...
        string range = "";
        var dt = DateTime.Now;

        for (int i = 0; i < length - 1; i++)
        {
            dt = DateTime.Now;
            range = $"Sheet44!A{i + 1}";
            var valueRange = new ValueRange();
            // Setting Cell Value...
            var oblist = new List<object>() { headers[i] };
            valueRange.Values = new List<IList<object>> { oblist };
            // Append the above record...
            var appendRequest = _sheetsService.Spreadsheets.Values.Append(valueRange, _spreadsheetId, range);
            appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
            //var appendReponse = appendRequest.Execute();
            do
            {

                

            } while (dt + TimeSpan.FromSeconds(1) > DateTime.Now);
            appendRequest.Execute();

            dt = DateTime.Now;
            range = $"Sheet44!B{i + 1}";
            var valueRange2 = new ValueRange();
            var oblist2 = new List<object>() { prices[i] };
            valueRange.Values = new List<IList<object>> { oblist2 };
            // Append the above record...
            appendRequest = _sheetsService.Spreadsheets.Values.Append(valueRange, _spreadsheetId, range);
            appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
            //var appendReponse = appendRequest.Execute();

            do
            {
                    
                
                
            } while (dt + TimeSpan.FromSeconds(1) > DateTime.Now);
            
            appendRequest.Execute();
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...