2017-04-18 44 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); 
});