在c#中为谷歌电子表格中的特定列写入值时出错
本文关键字:出错 谷歌 电子表格 | 更新日期: 2023-09-27 18:21:44
我在电子表格中手动定义了一列,名称为:
- 新建住宅
- 日期和时间
- M/F
如何为这些列写入值。我试过
row.Elements.Add(new ListEntry.Custom() { LocalName = "newlybuildhome?", Value = "Joe" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "m/f", Value = "f" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "dateandtime", Value = "26" });
service.Insert(listFeed, row);
我得到一个异常,不能写入空白行;请改用delete
我试过:
row.Elements.Add(new ListEntry.Custom() { LocalName = "Newly build home?", Value = "Joe" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "M/F", Value = "Smith" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "Date and Time", Value = "26" });
service.Insert(listFeed, row);
我得到了响应字符串的异常:属性名称"构建";与一个元素类型"相关联";gsx:新的";必须后跟';=';性格
我使用了其他过程来更改批次中的单元格值
API支持在单个请求中更新整个列、行或其他单元格集。这是为了提高性能,而不是提出大量的单独请求。这个过程被称为"批处理请求"
许多批处理操作可以组合成一个请求。支持的两种批处理操作是查询和更新。不支持插入和删除,因为单元格馈送不能用于插入或删除单元格。请记住,必须使用工作表提要来执行此操作。
查询和更新可以组合在任何大小的集合中,并在单个请求中发送到API。API通过批处理中每个单独操作的状态信息进行响应。对于查询请求,结果是请求的单元格。对于更新请求,结果包括具有新值的操作单元。
class BatchCellUpdater
{
// The number of rows to fill in the destination workbook
const int MAX_ROWS = 75;
// The number of columns to fill in the destination workbook
const int MAX_COLS = 5;
/**
* A basic struct to store cell row/column information and the associated RnCn
* identifier.
*/
private class CellAddress
{
public uint Row;
public uint Col;
public string IdString;
/**
* Constructs a CellAddress representing the specified {@code row} and
* {@code col}. The IdString will be set in 'RnCn' notation.
*/
public CellAddress(uint row, uint col)
{
this.Row = row;
this.Col = col;
this.IdString = string.Format("R{0}C{1}", row, col);
}
}
static void Main(string[] args)
{
string key;
// Command line parsing
if (args.Length != 1)
{
Console.Error.WriteLine("Syntax: BatchCellUpdater <key>");
return;
}
else
{
key = args[0];
}
// Prepare Spreadsheet Service
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
// TODO: Authorize the service object for a specific user (see other sections)
CellQuery cellQuery = new CellQuery(key, "od6", "private", "full");
CellFeed cellFeed = service.Query(cellQuery);
// Build list of cell addresses to be filled in
List<CellAddress> cellAddrs = new List<CellAddress>();
for (uint row = 1; row <= MAX_ROWS; ++row)
{
for (uint col = 1; col <= MAX_COLS; ++col)
{
cellAddrs.Add(new CellAddress(row, col));
}
}
// Prepare the update
// GetCellEntryMap is what makes the update fast.
Dictionary<String, CellEntry> cellEntries = GetCellEntryMap(service, cellFeed, cellAddrs);
CellFeed batchRequest = new CellFeed(cellQuery.Uri, service);
foreach (CellAddress cellAddr in cellAddrs)
{
CellEntry batchEntry = cellEntries[cellAddr.IdString];
batchEntry.InputValue = cellAddr.IdString;
batchEntry.BatchData = new GDataBatchEntryData(cellAddr.IdString, GDataBatchOperationType.update);
batchRequest.Entries.Add(batchEntry);
}
// Submit the update
CellFeed batchResponse = (CellFeed)service.Batch(batchRequest, new Uri(cellFeed.Batch));
// Check the results
bool isSuccess = true;
foreach (CellEntry entry in batchResponse.Entries)
{
string batchId = entry.BatchData.Id;
if (entry.BatchData.Status.Code != 200)
{
isSuccess = false;
GDataBatchStatus status = entry.BatchData.Status;
Console.WriteLine("{0} failed ({1})", batchId, status.Reason);
}
}
Console.WriteLine(isSuccess ? "Batch operations successful." : "Batch operations failed");
}
/**
* Connects to the specified {@link SpreadsheetsService} and uses a batch
* request to retrieve a {@link CellEntry} for each cell enumerated in {@code
* cellAddrs}. Each cell entry is placed into a map keyed by its RnCn
* identifier.
*
* @param service the spreadsheet service to use.
* @param cellFeed the cell feed to use.
* @param cellAddrs list of cell addresses to be retrieved.
* @return a dictionary consisting of one {@link CellEntry} for each address in {@code
* cellAddrs}
*/
private static Dictionary<String, CellEntry> GetCellEntryMap(
SpreadsheetsService service, CellFeed cellFeed, List<CellAddress> cellAddrs)
{
CellFeed batchRequest = new CellFeed(new Uri(cellFeed.Self), service);
foreach (CellAddress cellId in cellAddrs)
{
CellEntry batchEntry = new CellEntry(cellId.Row, cellId.Col, cellId.IdString);
batchEntry.Id = new AtomId(string.Format("{0}/{1}", cellFeed.Self, cellId.IdString));
batchEntry.BatchData = new GDataBatchEntryData(cellId.IdString, GDataBatchOperationType.query);
batchRequest.Entries.Add(batchEntry);
}
CellFeed queryBatchResponse = (CellFeed)service.Batch(batchRequest, new Uri(cellFeed.Batch));
Dictionary<String, CellEntry> cellEntryMap = new Dictionary<String, CellEntry>();
foreach (CellEntry entry in queryBatchResponse.Entries)
{
cellEntryMap.Add(entry.BatchData.Id, entry);
Console.WriteLine("batch {0} (CellEntry: id={1} editLink={2} inputValue={3})",
entry.BatchData.Id, entry.Id, entry.EditUri,
entry.InputValue);
}
return cellEntryMap;
}
}