【问题标题】:Setting format as percent in a cell using Google Sheets API使用 Google Sheets API 在单元格中将格式设置为百分比
【发布时间】:2017-09-15 15:44:52
【问题描述】:

我在创建单元格时尝试设置百分比格式,这是我的代码。

new Data.CellData
{
    UserEnteredValue = new Data.ExtendedValue
    {
        FormulaValue = "=(B2/C2)-1"
    },
    EffectiveFormat = new Data.CellFormat
    {
        NumberFormat = new Data.NumberFormat
        {
            Pattern = "00.0%",
            Type =  "NUMBER"
        }
    }
}

显示的当前值为“-0.2488570834”,我需要这个“-24.88%”。我错过了什么?

提前致谢。

【问题讨论】:

    标签: c# .net google-sheets-api


    【解决方案1】:

    尝试按照文档中提供的Number format examples

    我还找到了一个 sample code 来测试这个(将格式转换为您的示例):

     var ss = SpreadsheetApp.getActiveSpreadsheet();
      var range = ss.getActiveRange();
    
      var rows = range.getNumRows();
      var cols = range.getNumColumns();
      for(var row = 1; row <= rows; row++) {
        for(var col = 1; col <= cols; col++) {
          var cell = range.getCell(row, col);
          var value = cell.getValue();
          if(typeof(value) == 'number') {
            cell.setNumberFormat("##.#%");
          }
        }
      } 
    

    结果:

    代码示例适用于 Apps 脚本,但 C# 的实现是相同的。将Pattern = "00.0%", 更改为Pattern = "##.#%",

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助,但在我的情况下它没有用。我认为我的问题更多在于我发送的数据结构而不是字符串格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多