local p =
function p.calculateSum(frame) local args = frame.args local tableTag = args[1] -- The tag to identify the table local startColumn = tonumber(args[2]) or 2 local endColumn = tonumber(args[3]) or 4
local columnSums = for i = startColumn, endColumn do columnSums[i] = 0 end
-- Get the entire content of the current page local content = mw.title.getCurrentTitle:getContent
-- Handle the case where content might be nil if not content then return "Error: Page content could not be retrieved." end
-- Find the table based on the tag local tagPattern = "" local startPos, endPos = content:find(tagPattern)
-- Extract table content after the tag if not startPos then return "Error: Table tag not found." end
-- Extract table content following the tag local tableContent = content:sub(endPos + 1):match('
-- Initialize parsing variables local isInTable = false local currentRow = -- Function to process the collected row local function processRow(row) if isInTable then for i = startColumn, endColumn do if currentRow[i] then local cellValue = tonumber(mw.text.trim(currentRow[i])) if cellValue then columnSums[i] = columnSums[i] + cellValue end end end end end
-- Iterate through each line in the table content for row in tableContent:gmatch("[^\n]+") do row = mw.text.trim(row)
-- Detect the start and end of table rows if row:match('
-- Process the last row if needed if isInTable then processRow(currentRow) end
-- Create a string representation of the sums local sumString = "" for i = startColumn, endColumn do sumString = sumString .. "||" .. columnSums[i] end
return sumString:sub(3)end
return p