【问题标题】:Office Scripts (Sum Cells By Color)Office 脚本(按颜色对单元格求和)
【发布时间】:2023-02-03 00:50:33
【问题描述】:

我正在尝试转换一个代码,它在 VBA 中按颜色生成单元格总和,但我需要使用与 Office 脚本中的代码相同的代码或操作,我不知道这个平台的结构如何,也许,你能帮我做吗?

VBA中的代码是这样的:代码

Function SumByColor(Cellcolor As Range, RangeSum As Range) As Double

Dim cell As Range

For Each cell In RangeSum

If celda.Interior.ColorIndex = Celdacolor.Cells(1, 1).Interior.ColorIndex Then SumByColor = SumByColor+ cell

Next cell

Set cell = Nothing

End Function

所以我需要在办公脚本中使用这段代码

【问题讨论】:

  • 我没有使用 office 脚本的经验,但这是获取单元格颜色的方法:LINK
  • Office 脚本目前不支持函数。所以你不能写一个函数来做这个。

标签: excel vba office-scripts


【解决方案1】:

这是在 OfficeScript 中编写函数以及如何调用它的一种方法 -

function main(workbook: ExcelScript.Workbook) {
    let sheet = workbook.getActiveWorksheet();
    console.log (sumByColor(sheet.getRange("E41"), workbook.getSelectedRange()))
}

function sumByColor(cellColor:ExcelScript.Range, rangeSum:ExcelScript.Range):number {
    let value = 0;
    let rowCount = rangeSum.getRowCount();
    let columnCount = rangeSum.getColumnCount();
    let colorToCheck = cellColor.getFormat().getFill().getColor();

    // loop through each cell in rangeSum
    for (let row=0; row<rowCount; row++)
        for (let column = 0; column < columnCount; column++)
        {
            if (rangeSum.getCell(row,column).getFormat().getFill().getColor() == colorToCheck)
                value++;
        }
    return value;
}

【讨论】:

  • 嗨兄弟,非常感谢,但是如果我想获得具有特定颜色的单元格值的总和?我的意思是,只有单元格 C5 和 C6 分别具有值($12000 和 $20000),并且需要返回此值 (32000)。我该怎么做?获取单元格值并将该值求和
  • @santiagocarvajal 您可以尝试用类似value += rangeSum.getCell(row,column).getValue() as number 的内容替换value++ 部分。
【解决方案2】:

谢谢你的帮助。我的最终代码是:

function main(workbook: ExcelScript.Workbook) {
     let sheet = workbook.getActiveWorksheet();
     var cont = 2;
     const celdas = ['B10', 'C10', 'D10', 'E10', 'F10', 'G10', 'H10', 'I10',   'J10', 'K10', 'L10', 'M10'];
    celdas.forEach(celda => {
    let valCel = celda;
    let startingCell = sheet.getRange(valCel);
    var ranguito = "O" + cont.toString();
    let rangeDataValue = sheet.getRange(ranguito).getValue() as string;
    console.log(sumByColor(sheet.getRange("Q3"),      sheet.getRange(rangeDataValue), startingCell))
    cont = cont + 1;

});
}

 function sumByColor(cellColor: ExcelScript.Range, rangeSum:   ExcelScript.Range, writeCell: ExcelScript.Range) {
  let value = 0;
  let rowCount = rangeSum.getRowCount();
  let columnCount = rangeSum.getColumnCount();
let colorToCheck = cellColor.getFormat().getFill().getColor();
// loop through each cell in rangeSum
for (let row = 0; row < rowCount; row++)
    for (let column = 0; column < columnCount; column++) {
        if (rangeSum.getCell(row, column).getFormat().getFill().getColor() == colorToCheck) {
            var total = rangeSum.getCell(row, column).getValue() as string;
            value = value + parseFloat(total);

        }
   }
writeCell.setValue(value);
console.log(value)
}

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2020-07-02
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多