漂亮的打印 Excel 公式?

发布于 2024-08-04 17:58:47 字数 241 浏览 7 评论 0原文

有没有办法漂亮地打印Excel公式?

我有一些半复杂公式的工作表需要费力地完成,所以这会让我的生活变得更轻松一些。

我只是想将这样的东西变成

AC6+AD6+(IF(H6="Yes",1,IF(J6="Yes",1,0)))+IF(X6="Yes",1,0)

更具可读性的东西,而无需在 Vim 等中手动执行。 Excel 确实对括号进行了颜色匹配,但它仍然全部混合在一行上。

Is there a way to pretty print Excel formulas?

I've got a few worksheets of semi-complex formulas to slog through, so this would make my life a bit easier.

I'm just looking to turn something like this

AC6+AD6+(IF(H6="Yes",1,IF(J6="Yes",1,0)))+IF(X6="Yes",1,0)

into something more readable without manually doing it in Vim or the like. Excel does do color-matching on the parentheses, but it's still all mashed together on one line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一绘本一梦想 2024-08-11 17:58:47

尝试使用 Excel 公式美化器 http://excelformulabeautifier.com/。它可以漂亮地打印(又名美化)Excel 公式。

(我帮助维护这一点,总是寻求反馈以使其变得更好。)

Try Excel Formula Beautifier http://excelformulabeautifier.com/. It pretty prints (aka beautifies) Excel formulas.

(I help maintain this, always looking for feedback to make it better.)

踏雪无痕 2024-08-11 17:58:47

此 VBA 代码不会获奖,但可以快速查看典型公式。它只是执行您手动使用括号或分隔符执行的操作。将其粘贴到代码模块中并从 VBA 立即窗口命令行调用它。 (编辑:我最近不得不查看一些公式,并且我对原来答案的内容进行了改进,所以我回来并更改了它。)

Public Function ppf(f) As String
    Dim formulaStr As String

    If IsObject(f) Then
        Debug.Assert TypeOf f Is Range

        Dim rng As Range
        Set rng = f

        formulaStr = rng.Formula
    Else
        Debug.Assert VarType(f) = vbString

        formulaStr = f
    End If

    Dim tabs(0 To 99) As Long

    Dim tabNum As Long
    tabNum = 1

    Dim tabOffset As Long

    Dim i As Long
    Dim c As String
    For i = 1 To Len(formulaStr)
        c = Mid$(formulaStr, i, 1)

        If InStr("({", c) > 0 Then
            ppf = ppf & c

            tabNum = tabNum + 1
            tabs(tabNum) = tabs(tabNum - 1) + tabOffset + 1
            tabOffset = 0

            ppf = ppf & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr(")}", c) > 0 Then
            tabNum = tabNum - 1
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr("+-*/^,;", c) > 0 Then
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        Else
            ppf = ppf & c

            tabOffset = tabOffset + 1
        End If
    Next i
End Function

如果你这样称呼它:

?ppf([q42])

你不必担心转义你的双引号等等。您将得到如下所示的输出:

AC6+
AD6+
(
 IF(
    H6="Yes",
    1,
    IF(
       J6="Yes",
       1,
       0)
    )
 )
+
IF(
   X6="Yes",
   1,
   0)

您还可以使用纯字符串调用它。

This VBA code will win no awards, but it's O.K. for quickly looking at typical formulas. It just does what you'd do with parens or separators manually. Stick it in a code module and call it from the VBA immediate window command line. (EDIT: I had to look at some formulas recently, and I improved on what was here from my original answer, so I came back and changed it.)

Public Function ppf(f) As String
    Dim formulaStr As String

    If IsObject(f) Then
        Debug.Assert TypeOf f Is Range

        Dim rng As Range
        Set rng = f

        formulaStr = rng.Formula
    Else
        Debug.Assert VarType(f) = vbString

        formulaStr = f
    End If

    Dim tabs(0 To 99) As Long

    Dim tabNum As Long
    tabNum = 1

    Dim tabOffset As Long

    Dim i As Long
    Dim c As String
    For i = 1 To Len(formulaStr)
        c = Mid$(formulaStr, i, 1)

        If InStr("({", c) > 0 Then
            ppf = ppf & c

            tabNum = tabNum + 1
            tabs(tabNum) = tabs(tabNum - 1) + tabOffset + 1
            tabOffset = 0

            ppf = ppf & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr(")}", c) > 0 Then
            tabNum = tabNum - 1
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr("+-*/^,;", c) > 0 Then
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        Else
            ppf = ppf & c

            tabOffset = tabOffset + 1
        End If
    Next i
End Function

If you call it like so:

?ppf([q42])

you don't have to worry about escaping your double quotes and so on. You'll get output that looks like this:

AC6+
AD6+
(
 IF(
    H6="Yes",
    1,
    IF(
       J6="Yes",
       1,
       0)
    )
 )
+
IF(
   X6="Yes",
   1,
   0)

You can also call it with a plain string.

梦幻的味道 2024-08-11 17:58:47

到 2023 年,迄今为止最简单的方法是安装微软自己的高级公式环境。除了漂亮的打印公式(然后可以将其复制粘贴到常规公式栏中)之外,它还具有许多其他功能。转到“插入”选项卡,“获取加载项”,搜索“Excel Labs”。

我还将衷心推荐“Formula Forge”插件作为评估公式的替代品。它使困难的公式更容易评估。

In 2023, by far the easiest way to do this is to install Microsoft's own Advanced Formula Environment. As well as pretty printing formulas (which can then be copy-pasted into the regular formula bar), it has a host of other features. Go to 'Insert' tab, 'Get Add-ins', search 'Excel Labs'.

I'll also heartily recommend the 'Formula Forge' add-in as a replacement for Evaluate Formula. It makes difficult formulae much, much easier to evaluate.

帅哥哥的热头脑 2024-08-11 17:58:47

上述代码片段的这个版本现在也以不同的方式处理引用的字符,这意味着,它不会影响它们,如果它们位于字符串内部,则不会让它们影响缩进,例如:

"This ({)},;+*-/ won't lead to a linebreak" 

它由布尔变量 bInsideQuotes 控制。
它还用于

.FormulaLocal

使最终用户能够看到他们知道的东西。

Public Function prettyPrintFormula(f As Variant) As String
    Dim formulaStr As String
    Dim ppf As String

    If IsObject(f) Then
        Debug.Assert TypeOf f Is Range

        Dim rng As Range
        Set rng = f

        formulaStr = rng.FormulaLocal
    Else
        Debug.Assert VarType(f) = vbString

        formulaStr = f
    End If

    Dim tabs(0 To 99) As Long

    Dim tabNum As Long
    tabNum = 1

    Dim tabOffset As Long

    Dim i As Long
    Dim c As String
    Dim bInsideQuotes As Boolean
    bInsideQuotes = False
    For i = 1 To Len(formulaStr)
        c = Mid$(formulaStr, i, 1)

        If InStr("""", c) > 0 Then
            bInsideQuotes = Not bInsideQuotes
        End If
        If InStr("({", c) > 0 And Not bInsideQuotes Then
            ppf = ppf & c

            tabNum = tabNum + 1
            tabs(tabNum) = tabs(tabNum - 1) + tabOffset + 1
            tabOffset = 0

            ppf = ppf & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr(")}", c) > 0 And Not bInsideQuotes Then
            tabNum = tabNum - 1
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr("+-*/^,;", c) > 0 And Not bInsideQuotes Then
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        Else
            ppf = ppf & c

            tabOffset = tabOffset + 1
        End If
    Next i
    prettyPrintFormula = ppf
End Function

This version of the above code snippet now also handels quoted characters differently, which means, it leaves them alone and doesn't let them effect the indenting if they are inside a string like:

"This ({)},;+*-/ won't lead to a linebreak" 

It is controlled by the boolean variable bInsideQuotes.
It also uses

.FormulaLocal

to make it possible for Endusers to see something they know.

Public Function prettyPrintFormula(f As Variant) As String
    Dim formulaStr As String
    Dim ppf As String

    If IsObject(f) Then
        Debug.Assert TypeOf f Is Range

        Dim rng As Range
        Set rng = f

        formulaStr = rng.FormulaLocal
    Else
        Debug.Assert VarType(f) = vbString

        formulaStr = f
    End If

    Dim tabs(0 To 99) As Long

    Dim tabNum As Long
    tabNum = 1

    Dim tabOffset As Long

    Dim i As Long
    Dim c As String
    Dim bInsideQuotes As Boolean
    bInsideQuotes = False
    For i = 1 To Len(formulaStr)
        c = Mid$(formulaStr, i, 1)

        If InStr("""", c) > 0 Then
            bInsideQuotes = Not bInsideQuotes
        End If
        If InStr("({", c) > 0 And Not bInsideQuotes Then
            ppf = ppf & c

            tabNum = tabNum + 1
            tabs(tabNum) = tabs(tabNum - 1) + tabOffset + 1
            tabOffset = 0

            ppf = ppf & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr(")}", c) > 0 And Not bInsideQuotes Then
            tabNum = tabNum - 1
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        ElseIf InStr("+-*/^,;", c) > 0 And Not bInsideQuotes Then
            tabOffset = 0

            ppf = ppf & c & vbCrLf & Space(tabs(tabNum))
        Else
            ppf = ppf & c

            tabOffset = tabOffset + 1
        End If
    Next i
    prettyPrintFormula = ppf
End Function
疑心病 2024-08-11 17:58:47

我刚刚使用 VS Code sql-formatter 扩展。我只需将公式粘贴到 sql 文件并使用 sql 格式化程序进行格式化。

当我将其归档时,

{=IFERROR(INDEX(names,SMALL(IF(groups=$E5,ROW(names)-MIN(ROW(names))+1),COLUMNS($E$5:E5))),"")}

结果是这样的:

{ = IFERROR(
  INDEX(
    NAMES,
    SMALL(
      IF(groups = $ E5, ROW(NAMES) - MIN(ROW(NAMES)) + 1),
      COLUMNS($ E $ 5 :E5)
    )
  ),
  ""
) }

它并不完美,但符合我的口味。并且可以将其复制并重新使用到 Excel / google 表格中。

I just solved the issue by using the VS Code sql-formatter extension. I simply paste my formular to a sql file and format with the sql-formatter.

When I put this to file

{=IFERROR(INDEX(names,SMALL(IF(groups=$E5,ROW(names)-MIN(ROW(names))+1),COLUMNS($E$5:E5))),"")}

It comes out like that:

{ = IFERROR(
  INDEX(
    NAMES,
    SMALL(
      IF(groups = $ E5, ROW(NAMES) - MIN(ROW(NAMES)) + 1),
      COLUMNS($ E $ 5 :E5)
    )
  ),
  ""
) }

Its not perfect but acceptable for my taste. And this can be copied and used back into Excel / google sheets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文