<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mbwiki.stairwaygames.work/w/index.php?action=history&amp;feed=atom&amp;title=Module%3AAutocalctable</id>
	<title>Module:Autocalctable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mbwiki.stairwaygames.work/w/index.php?action=history&amp;feed=atom&amp;title=Module%3AAutocalctable"/>
	<link rel="alternate" type="text/html" href="https://mbwiki.stairwaygames.work/w/index.php?title=Module:Autocalctable&amp;action=history"/>
	<updated>2026-04-07T18:19:22Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://mbwiki.stairwaygames.work/w/index.php?title=Module:Autocalctable&amp;diff=5673&amp;oldid=prev</id>
		<title>Admin coral island: Created page with &quot;--|Description : Autocalctable --|Adds summation/average/min/max values of a column of a table imported from json/module/csv page --|Syntax : --|{{#invoke:Autotablecalc|page=pagename|type=datatype|calc=calculationtype}} --|Where calc = sum/avg/min/max - can be more than one separated by commas ,e.g. calc=sum,avg --| pagename = name of page/module --| datatype = json or lua or cvs  local p = {} local csv = require(&quot;Dev:Csv&quot;) local json = require(&quot;Dev:Json&quot;) local getArgs...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mbwiki.stairwaygames.work/w/index.php?title=Module:Autocalctable&amp;diff=5673&amp;oldid=prev"/>
		<updated>2023-08-03T16:56:55Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;--|Description : Autocalctable --|Adds summation/average/min/max values of a column of a table imported from json/module/csv page --|Syntax : --|{{#invoke:Autotablecalc|page=pagename|type=datatype|calc=calculationtype}} --|Where calc = sum/avg/min/max - can be more than one separated by commas ,e.g. calc=sum,avg --| pagename = name of page/module --| datatype = json or lua or cvs  local p = {} local csv = require(&amp;quot;Dev:Csv&amp;quot;) local json = require(&amp;quot;Dev:Json&amp;quot;) local getArgs...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--|Description : Autocalctable&lt;br /&gt;
--|Adds summation/average/min/max values of a column of a table imported from json/module/csv page&lt;br /&gt;
--|Syntax :&lt;br /&gt;
--|{{#invoke:Autotablecalc|page=pagename|type=datatype|calc=calculationtype}}&lt;br /&gt;
--|Where calc = sum/avg/min/max - can be more than one separated by commas ,e.g. calc=sum,avg&lt;br /&gt;
--| pagename = name of page/module&lt;br /&gt;
--| datatype = json or lua or cvs&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
local csv = require(&amp;quot;Dev:Csv&amp;quot;)&lt;br /&gt;
local json = require(&amp;quot;Dev:Json&amp;quot;)&lt;br /&gt;
local getArgs = require(&amp;quot;Dev:Arguments&amp;quot;).getArgs&lt;br /&gt;
&lt;br /&gt;
--%  Extracts table columns containing numbers&lt;br /&gt;
--@ sPage (string) A page containing the table e.g Module:Scores.json&lt;br /&gt;
--@ sType (string) The type of data contained in the page (csv, json, lua) -lua table&lt;br /&gt;
--: (string) A table containing calculated columns&lt;br /&gt;
local function getTable(sPage, sType)&lt;br /&gt;
	if sPage then&lt;br /&gt;
		if sType == &amp;quot;lua&amp;quot; then&lt;br /&gt;
			return require(sPage)&lt;br /&gt;
		elseif sType == &amp;quot;csv&amp;quot; then&lt;br /&gt;
			return csv.convertToLua(sPage)&lt;br /&gt;
		end&lt;br /&gt;
		local oData = mw.title.new(sPage):getContent()&lt;br /&gt;
		if oData and sType == &amp;quot;json&amp;quot; then&lt;br /&gt;
			return json.decode(oData)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--% Entry point - Creates a table with automatically calculated columns&lt;br /&gt;
--@ frame (table) A scribunto frame&lt;br /&gt;
--: (string) A table containing calculated columns&lt;br /&gt;
function p.main(frame)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	local sPageName = args.page&lt;br /&gt;
	local sType = args.type&lt;br /&gt;
	local calculations = args.calc&lt;br /&gt;
	local calcTable = getTable(sPageName, sType)&lt;br /&gt;
	&lt;br /&gt;
	return p.createHtmlTable(calcTable, calculations)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--% Creates an html table&lt;br /&gt;
--@ arrColumns (string) A column containing numbers&lt;br /&gt;
--@ calculations (string) The types of calculations to perform (avg, min, max, sum)&lt;br /&gt;
--: (string) An html table containing calculated columns&lt;br /&gt;
function p.createHtmlTable(arrColumns, calculations)&lt;br /&gt;
	if not arrColumns then&lt;br /&gt;
		return&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local hTable = mw.html.create(&amp;quot;table&amp;quot;)&lt;br /&gt;
	local storedData = {}&lt;br /&gt;
	local calc = {sum = {0}, avg = {0}, min = {0}, max = {0}, elements = {}}&lt;br /&gt;
	&lt;br /&gt;
	for i in ipairs(arrColumns) do &lt;br /&gt;
		local sTag = i == 1 and &amp;quot;th&amp;quot; or &amp;quot;td&amp;quot;&lt;br /&gt;
		hTable:tag(&amp;quot;tr&amp;quot;)&lt;br /&gt;
		&lt;br /&gt;
		for sKey, sData in ipairs(arrColumns[i]) do&lt;br /&gt;
			storedData[sKey] = storedData[sKey] or {}&lt;br /&gt;
			&lt;br /&gt;
			hTable:tag(sTag)&lt;br /&gt;
				:wikitext(sData)&lt;br /&gt;
			-- //gets second row because the first one is the heading&lt;br /&gt;
			if tonumber(sData) and i &amp;gt; 1 then&lt;br /&gt;
				storedData[sKey][#storedData[sKey] + 1] = tonumber(sData)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	local val&lt;br /&gt;
	&lt;br /&gt;
	for index, dataTable in pairs(storedData) do&lt;br /&gt;
		for _, value in pairs(dataTable) do&lt;br /&gt;
			val = value or 0&lt;br /&gt;
			calc.sum[index] = (calc.sum[index] or 0) + val&lt;br /&gt;
			calc.min[index] = calc.min[index] or 0&lt;br /&gt;
			calc.max[index] = calc.max[index] or 0&lt;br /&gt;
			calc.avg[index] = calc.avg[index] or 0&lt;br /&gt;
			&lt;br /&gt;
			if calc.min[index] &amp;gt;= val then&lt;br /&gt;
				calc.min[index] = val&lt;br /&gt;
			end&lt;br /&gt;
			&lt;br /&gt;
			if calc.max[index] &amp;lt;= val then&lt;br /&gt;
				calc.max[index] = val&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		&lt;br /&gt;
		calc.elements[index] = #dataTable&lt;br /&gt;
		&lt;br /&gt;
		if calc.sum[index] &amp;gt; 0 then&lt;br /&gt;
			calc.avg[index] = calc.sum[index] / #dataTable&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	local calcTable = mw.text.split(calculations or &amp;quot;&amp;quot;, &amp;quot;,&amp;quot;)&lt;br /&gt;
	local bFlag = true&lt;br /&gt;
	local trNode&lt;br /&gt;
	&lt;br /&gt;
	for _, calctype in pairs(calcTable) do&lt;br /&gt;
		if calc[calctype] then&lt;br /&gt;
			trNode = mw.html.create(&amp;quot;tr&amp;quot;)&lt;br /&gt;
			&lt;br /&gt;
			for sKey in ipairs(arrColumns[1]) do&lt;br /&gt;
				trNode:tag(&amp;quot;td&amp;quot;)&lt;br /&gt;
					:wikitext(calc[calctype][sKey])&lt;br /&gt;
			end&lt;br /&gt;
			&lt;br /&gt;
			if bFlag then&lt;br /&gt;
				trNode:css(&amp;quot;border-top&amp;quot;, &amp;quot;thick double #ff0000&amp;quot;)&lt;br /&gt;
				bFlag = false&lt;br /&gt;
			end&lt;br /&gt;
			hTable:node(trNode):done()&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	hTable:addClass(&amp;quot;wikitable&amp;quot;)&lt;br /&gt;
	hTable:done()&lt;br /&gt;
	&lt;br /&gt;
	return hTable&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Admin coral island</name></author>
	</entry>
</feed>