【问题标题】:Unable to add column between two columns using Google spreadsheet Api无法使用 Google 电子表格 Api 在两列之间添加列
【发布时间】:2017-09-26 05:04:05
【问题描述】:

我有一个 Google 电子表格。在我的 E 列上,如果 E 列上不可用,我想添加最近的月份(例如:2017 年 9 月)。假设有(2017 年 8 月),现在当我添加(2017 年 9 月)时,它会添加但它会替换现有的列是 2107 年 8 月。我的要求是同时保留:新添加的(2017 年 9 月)和(2017 年 8 月)。 我想最近一个月在 E 列上,并在右侧移动现有列,即 F 列。我正在使用 C# 执行此操作。

static void Main(string[] args)
{
  HttpClient client = new HttpClient();
  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

  try
  {
    var service = new SheetsService(new BaseClientService.Initializer()
    {
      HttpClientInitializer = GetCredential(),
      ApplicationName = ApplicationName,
    });
    String spreadsheetId = "1lZnvQe6lTGG81hyuQvf7HjH8YpnIadFlNHjFUq_G-5Q";
    String range = "E1";

    SpreadsheetsResource.ValuesResource.GetRequest getRequest = service.Spreadsheets.Values.Get(spreadsheetId, range);
    Data.ValueRange response = getRequest.Execute();

    string recentMonth = DateTime.Now.ToString("MMM yyyy");
    var recentMonthArrayValue = new List<string[]>();
    recentMonthArrayValue.Add(new string[] { recentMonth });
    if (response.Values[0][0].ToString() != recentMonth)
    {
      DateTime now = DateTime.Now;
      var currentMonthStartDate = new DateTime(now.Year, now.Month, 1).ToShortDateString();

      Data.BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new Data.BatchUpdateSpreadsheetRequest();
      batchUpdateSpreadsheetRequest.Requests = new List<Data.Request>();
      Data.Request request = new Data.Request();
      batchUpdateSpreadsheetRequest.Requests.Add(request);
      request.UpdateCells = new Data.UpdateCellsRequest();

      var gridCoordinate = new Data.GridCoordinate();
      gridCoordinate.ColumnIndex = 5;
      gridCoordinate.SheetId = 0;
      request.UpdateCells.Start = gridCoordinate;
      request.UpdateCells.Fields = "*";
      request.UpdateCells.Rows = new List<Data.RowData>();
      var rowData = new Data.RowData();
      request.UpdateCells.Rows.Add(rowData);
      rowData.Values = new List<Data.CellData>();
      var cellData = new Data.CellData();
      cellData.UserEnteredValue = new Data.ExtendedValue();
      cellData.UserEnteredValue.FormulaValue = "=TEXT(\"" + currentMonthStartDate + "\",\"MMM yyyy\")";
      rowData.Values.Add(cellData);
      SpreadsheetsResource.BatchUpdateRequest batchUpdateRequest = service.Spreadsheets.BatchUpdate(batchUpdateSpreadsheetRequest, spreadsheetId);
      batchUpdateRequest.Execute();
    }
  }
  catch (Exception e)
  {
    //throw;
  }
}
public static UserCredential GetCredential()
{
  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;
    return credential;
  }
}

【问题讨论】:

    标签: google-spreadsheet-api


    【解决方案1】:

    查看 Sheets API 中的 insert an empty row or column 指南:

    以下电子表格.batchUpdate 请求插入两个空白 列 C 的列。第二个请求在行插入三个空行 1.inheritBefore 字段,如果为真,则告诉 API 赋予新列或新行与先前行或列相同的属性; 否则新的列或行将获得那些的属性 跟着他们。如果在第 1 行插入一行,则 inheritBefore 不能为真 或 A 列中的一列。

    请求协议如下所示。更新电子表格指南 展示了如何使用不同语言实现批量更新 Google API 客户端库。

    POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
    
    
    {
      "requests": [
        {
          "insertDimension": {
            "range": {
              "sheetId": sheetId,
              "dimension": "COLUMNS",
              "startIndex": 2,
              "endIndex": 4
            },
            "inheritBefore": true
          }
        },
        {
          "insertDimension": {
            "range": {
              "sheetId": sheetId,
              "dimension": "ROWS",
              "startIndex": 0,
              "endIndex": 3
            },
            "inheritBefore": false
          }
        },
      ],
    }
    

    【讨论】:

    • 是的,我知道,但我无法用 c# 来做。如果你知道如何用 c# 做,请告诉我。
    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多