Search code examples
c#google-sheetsgoogle-sheets-api

How to change column height in Google sheets C#


I'm trying to modify the row height in Google sheets of a row I just added. The row consists of a list of strings (i.e. cells). First I add the row, then I try to modify the height:

// I need to convert my list of strings into a list of objects
// there's probably a better way to do this...
var valueRange = new ValueRange();
var oblist = new List<object>();
foreach (var thing in columns)
{
    oblist.Add(thing);
}
valueRange.Values = new List<IList<object>>() { oblist };
var appendRequest = service.Spreadsheets.Values.Update(valueRange, spreadsheetId, $"{sheetname}!A{rownumber}:Z{rownumber}");
appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
                    
var appendResponse = appendRequest.Execute();
                    

// now change the row height to 100 pixels.
BatchUpdateSpreadsheetRequest r = new BatchUpdateSpreadsheetRequest();
UpdateDimensionPropertiesRequest request = new UpdateDimensionPropertiesRequest());

request.Range = new DimensionRange();
request.Range.SheetId = sheetId;
request.Range.Dimension = "ROWS";
request.Range.StartIndex = 
request.Range.EndIndex = rownumber;
request.Properties = new DimensionProperties();
request.Properties.PixelSize = 100;
request.Fields = "pixelSize";
r.Requests.Add(request);
var bu = service.Spreadsheets.BatchUpdate(r, spreadsheetId);
bu.Execute();

The problem is in the third-last line where I can't add request to the Requests list, as request is not derived from the Request class.


Solution

  • Instead of trying to add the request directly to the batch, I need to add a Requests list to the batch, and then add the request to the Requests list:

    UpdateDimensionPropertiesRequest request = new UpdateDimensionPropertiesRequest();
    request.Range = new DimensionRange();
    request.Range.SheetId = sheetId;
    request.Range.Dimension = "ROWS";
    request.Range.StartIndex = firstrow;
    request.Properties = new DimensionProperties();
    request.Properties.PixelSize = 100;
    request.Fields = "pixelSize";
    
    // now add the request to the batch
    BatchUpdateSpreadsheetRequest busr = new BatchUpdateSpreadsheetRequest();
    busr.Requests = new List<Request>();
    busr.Requests.Add(new Request { UpdateDimensionProperties = request });
    var bur = service.Spreadsheets.BatchUpdate(busr, spreadsheetId);
    bur.Execute();