The code you show should work. I fleshed it out to a working example(*) and it shows the correct MAX and AVERAGE for valid cell ranges. If the cell range is empty then the result will be zero/#DIV0! like the corresponding Excel formula:

Spreadsheet::WriteExcel writes formulas in the required binary RPN format but it doesn't write the result of the formula since it wouldn't be practical to calculate the result of arbitrary formulae. So instead it writes 0 as a default formula result. Excel and most other spreadsheet applications recalculate the result of formulas when the file is loaded so that generally isn't an issue.

However, Excel Viewer doesn't calculate formulae so all you see is the default 0 result.

You can work around this by explicitly specifying the calculated value of the formula at the end of the argument list: