local p = local lang = mw.getContentLanguagelocal navbar = require("Module:Navbar")
local messages =
local bgColors =
local colors =
function p._cell(args) local data = args.data or mw.ext.data.get(args[1]) local rowIdx = tonumber(args.output_row) local outputFormat = args.output_format local outputColumnNames = while args["output_column" .. #outputColumnNames + 1] do table.insert(outputColumnNames, args["output_column" .. #outputColumnNames + 1]) end local outputColumnIdxs = local numOutputColumnIdxs = 0 for i, field in ipairs(data.schema.fields) do for j, outputColumnName in ipairs(outputColumnNames) do if field.name
#outputColumnNames then break end end if numOutputColumnIdxs < #outputColumnNames then for i, outputColumnName in ipairs(outputColumnNames) do assert(outputColumnIdxs[outputColumnName], mw.ustring.format("Output column “%s” not found.", outputColumnName)) end end if rowIdx > 0 then rowIdx = (rowIdx - 1) % #data.data + 1 elseif rowIdx < 0 then rowIdx = rowIdx % #data.data + 1 else error("0 is not a valid row index.") end local record = data.data[rowIdx] if record ~= nil then if outputFormat or numOutputColumnIdxs > 1 then local values = for i, columnName in ipairs(outputColumnNames) do local columnIdx = outputColumnIdxs[columnName] table.insert(values, record[columnIdx]) end if outputFormat then return mw.ustring.format(outputFormat, unpack(values)) else return mw.text.listToText(values) end else local columnIdx = outputColumnIdxs[outputColumnNames[1]] return record[columnIdx] end endend
--- Returns the value of the cell at the given row index and column name.--- A row index of 1 refers to the first row in the table. A row index of -1--- refers to the last row in the table. It is an error to specify a row index--- of 0.--- Usage: function p.cell(frame) return p._cell(frame.args)end
function p._lookup(args)
--local data = args.data or mw.ext.data.get(args[1]) local page = mw.text.trim(args[1]) -- "UN:Total population, both sexes combined.tab" -- set page name explicitly for testing local data = args.data or mw.ext.data.get(page)
local searchValue = args.search_value local searchPattern = args.search_pattern local searchColumnName = args.search_column local searchColumnIdx for i, field in ipairs(data.schema.fields) do if field.name
searchValue) or (searchPattern and mw.ustring.match(tostring(record[searchColumnIdx]), searchPattern)) then numMatchingRecords = numMatchingRecords + 1 if numMatchingRecords
--local data = args.data or mw.ext.data.get(args[1]) local page = mw.text.trim(args[1]) -- "UN:Total population, both sexes combined.tab" -- set page name explicitly for testing local data = args.data or mw.ext.data.get(page)
local searchValue = args.search_value local searchPattern = args.search_pattern local searchColumnName = args.search_column local searchValue2 = args.search_value2 local searchPattern2 = args.search_pattern2 local searchColumnName2 = args.search_column2 local searchColumnIdx for i, field in ipairs(data.schema.fields) do if field.name
local searchColumnIdx2 for i, field in ipairs(data.schema.fields) do if field.name
searchValue) and (searchValue2 and tostring(record[searchColumnIdx2])
1) then return data.data[i][3] end -- just return single occurence numMatchingRecords = numMatchingRecords + 1 if numMatchingRecords
--- Returns the value of the cell(s) in the given output column(s) of the row--- matching the search key and column.--- Reminiscent of LOOKUP macros in popular spreadsheet applications, except--- that the search key must match exactly. (On the other hand, this means the--- table does not need to be sorted.)--- Usage: function p.lookup(frame) return p._lookup(frame.args)end
--- As p.lookup except requiring match of values in two columns--- Usage: function p.lookup2(frame) --return p._lookup2(frame.args) return p.lookup2_minimal(frame.args) end-- version for testing resourcesfunction p.lookup2_minimal(args) --local page = mw.text.trim(args[1]) -- "UN:Total population, both sexes combined.tab" -- set page name explicitly for testing local data = args.data or mw.ext.data.get("UN:Total population, both sexes combined.tab")
local searchValue = args.search_value --local searchPattern = args.search_pattern --local searchColumnName = args.search_column local searchValue2 = args.search_value2 --local searchPattern2 = args.search_pattern2 --local searchColumnName2 = args.search_column2 local searchColumnIdx = 1 local searchColumnIdx2 = 2
--local occurrence = tonumber(args.occurrence) or 1 local numMatchingRecords = 0 for i = 1, #data.data, 1 do local record = data.data[i] if (searchValue and tostring(record[searchColumnIdx])
searchValue2) then -- or (searchPattern and mw.ustring.match(tostring(record[searchColumnIdx]), searchPattern)) then return data.data[i][3] -- just return single occurence
end endend
function p._wikitable(args) local pageName = args[1] local data = mw.ext.data.get(pageName) local datatypes = local htmlTable = mw.html.create("table") :addClass("wikitable sortable") htmlTable :tag("caption") :wikitext(navbar.navbar) :wikitext(data.description) local headerRow = htmlTable :tag("tr") for i, field in ipairs(data.schema.fields) do headerRow :tag("th") :attr("scope", "col") :attr("data-sort-type", datatypes[j]
"number" then formattedData = lang:formatNum(formattedData) cell:attr("align", "right") elseif datatypes[j]
--- Returns a tabular data page as a wikitext table.--- Usage: function p.wikitable(frame) return p._wikitable(frame.args)end
return p