2017-04-18 90 views
1

我试图在单元格上应用水平格式,但它不起作用。单元格格式化 - horizo​​ntalAlignment

其余的所有格式如字体颜色工作正常。

主机 - Office 365中,平台 - Excel中

码 -

Excel.run(function(ctx) { 
 

 
    var tableRange = ctx.workbook.tables.getItem(tableName).convertToRange(); 
 
    tableRange.load("values"); 
 

 
    return ctx.sync() 
 
    .then(function() { 
 
     for (var i = 0; i < tableRange.values.length; i++) { 
 
     for (var j = 0; j < tableRange.values[i].length; j++) { 
 
      if (tableRange.values[i][j] == 'somecondition') { 
 
      tableRange.getCell(i, j).values = [ 
 
       ['n'] 
 
      ]; 
 
      tableRange.getCell(i, j).format.font.color = "#ff0000"; 
 
      tableRange.getCell(i, j).horizontalAlignment = 'Center'; 
 

 
      } 
 

 
     } 
 
     } 
 
    }) 
 
    .then(ctx.sync); 
 
}).catch(function(error) { 
 
    console.log(error); 
 
});

回答

0

horizontalAlignmentRangeFormat属性:你

tableRange.getCell(i, j).format.horizontalAlignment = 'Center'; 

完整代码会t因此是:

Excel.run(function(ctx) { 

    var tableRange = ctx.workbook.tables.getItem(tableName).convertToRange(); 
    tableRange.load("values"); 

    return ctx.sync() 
    .then(function() { 
     for (var i = 0; i < tableRange.values.length; i++) { 
     for (var j = 0; j < tableRange.values[i].length; j++) { 
      if (tableRange.values[i][j] == 'somecondition') { 
      tableRange.getCell(i, j).values = [ 
       ['n'] 
      ]; 
      tableRange.getCell(i, j).format.font.color = "#ff0000"; 
      tableRange.getCell(i, j).horizontalAlignment = 'Center'; 
      }  
     } 
     } 
    }) 
    .then(ctx.sync); 
}).catch(function(error) { 
    console.log(error); 
});