【问题标题】:C# GoogleSheets Update Request not ExecutingC# GoogleSheets 更新请求未执行
【发布时间】:2020-05-06 03:58:18
【问题描述】:

我正在尝试更新 Google 电子表格中的值,代码执行到 addRequest.Execute(); 但是,它不运行执行语句。 如果我运行追加请求,这确实有效,但是我没有尝试追加,我正在尝试更新。 我有以下程序的范围static readonly string[] Scope = { SheetsService.Scope.Spreadsheets, DriveService.Scope.Drive};

var range = $"{ClashImport[i][0].ToString()}!B7:F106";
var REALInsertList = new sData.ValueRange();
var InsertList = new List<object>();

for (int n = 0; n < DataImport[i].Count; n++) {
    InsertList.Add(DataImport[i][n].AccountName);
    InsertList.Add(DataImport[i][n].AccountID);
    InsertList.Add(DataImport[i][n].Banned);
    InsertList.Add(DataImport[i][n].Suspended);
    InsertList.Add(DataImport[i][n].History);
}

REALInsertList.Values = new List<IList<object>> { InsertList };
var addRequest = sheetsService.Spreadsheets.Values.Update(REALInsertList, SheetToImportTo, range);
addRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
addRequest.Execute();

【问题讨论】:

    标签: c# google-sheets-api


    【解决方案1】:

    这个例子将帮助你实现你想要做的事情:

     // Define request parameters.
     // The ID of the spreadsheet to update.
     string spreadsheetId = "YOUR-SPREADSHEET-ID";  // TODO: Update placeholder value.
    
     // How the input data should be interpreted.
     string valueInputOption = "RAW";  // TODO: Update placeholder value. Ex -> RAW
    
     // The new values to apply to the spreadsheet.
     List<ValueRange> data = new List<ValueRange>(); // Instanciate a list of type ValueRange
     ValueRange values = new ValueRange(); // Instanciate a ValueRange object
     values.Range = "A1:B2"; // The range you want to update
     // Depending in your number of rows, create some logic to populate them 
     List<object> firstRow = new List<object> { "Hello", 2};
     List<object> secondRow = new List<object> { 3, "Hey!"};
     // Populate the values to be inserted in the sheet
     values.Values = new List<IList<object>> { firstRow, secondRow };
     // add values to the data ValueRange List
     data.Add(values);
    
     // TODO: Assign values to desired properties of `requestBody`:
     BatchUpdateValuesRequest requestBody = new BatchUpdateValuesRequest();
     requestBody.ValueInputOption = valueInputOption;
     requestBody.IncludeValuesInResponse = true;
     requestBody.Data = data;
    
     // Build and make the request
     SpreadsheetsResource.ValuesResource.BatchUpdateRequest request 
         = service.Spreadsheets.Values.BatchUpdate(requestBody, spreadsheetId);
     BatchUpdateValuesResponse response = request.Execute();
     IList<IList<object>> updatedValues = response.Responses[0].UpdatedData.Values;
    
     // Print updated values 
     Console.WriteLine("These are the updated values");
     foreach (var row in updatedValues)
     {
         Console.WriteLine("{0}, {1}", row[0], row[1]);
     }
    
     Console.Read();
    

    按照Method: spreadsheets.values.batchUpdate 端点文档和试试这个API,我能够了解如何构建请求更新主体。 请注意,我创建了一个 List&lt;ValueRange&gt;,我用适当的数据填充它并以这种方式发出请求。

    文档

    更多信息,您可以查看:

    【讨论】:

    • 谢谢!我对电子表格的整个写作过程都很陌生。这行得通! (感谢 cmets,帮助我了解我需要做什么)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    相关资源
    最近更新 更多