Module:Get cell: Difference between revisions
Appearance
Content deleted Content added
Sophivorus (talk | contribs) No edit summary |
Sophivorus (talk | contribs) No edit summary |
||
Line 50: | Line 50: | ||
local value = rowData[ number ] |
local value = rowData[ number ] |
||
local lang = mw.language.getContentLanguage() |
local lang = mw.language.getContentLanguage() |
||
value = lang:parseFormattedNumber( |
value = lang:parseFormattedNumber( ) |
||
return |
return |
||
end |
end |
||
Revision as of 12:29, 30 April 2024
-- This module serves to calculate a table cell out of the values of other cells
-- Documentation: https://en.wikipedia.org/wiki/Module:Calculated_cell
-- Authors: Sophivorus
-- License: GNU General Public License 3 or later (http://www.gnu.org/licenses/gpl-3.0.html)
local CalculatedCell = {}
local WikitextParser = require( 'Module:WikitextParser' )
local function getError( key )
local messages = {
['page-not-found'] = 'Page not found',
['page-not-valid'] = 'Invalid title',
['table-not-given'] = 'No table id given',
['table-not-found'] = 'Table not found',
['reference-not-given'] = 'No cell reference given',
['reference-not-valid'] = 'Invalid expression',
}
local frame = mw.getCurrentFrame()
local message = frame.args[ key ] or messages[ key ]
return mw.html.create( 'span' ):addClass( 'error' ):wikitext( message )
end
function CalculatedCell.main( frame )
-- Get the template parameters
local params = frame:getParent().args
-- Get the page wikitext
local pageTitle = mw.title.getCurrentTitle()
local page = params[3]
if page then pageTitle = mw.title.new( page ) end
if not pageTitle then return getError( 'page-not-valid' ) end
local pageWikitext = pageTitle:getContent()
if not pageWikitext then return getError( 'page-not-found' ) end
-- Get the table data
local tableId = params[2]
if not tableId then return getError( 'table-not-given' ) end
local tableWikitext = WikitextParser.getTableById( pageWikitext, tableId )
local tableData = WikitextParser.getTableData( tableWikitext )
-- Replace the reference (A1, B2, etc) for the real value
local reference = params[1]
if not reference then getError( 'reference-not-given' ) end
if not string.match( reference, '[A-Z]+[0-9]+' ) then getError( 'reference-not-valid' ) end
local letter = string.match( reference, '[A-Z]+' )
local letterIndex = string.find( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', letter )
local rowData = tableData[ letterIndex ]
local number = tonumber( string.match( reference, '[0-9]+' ) )
local value = rowData[ number ]
local lang = mw.language.getContentLanguage()
value = lang:parseFormattedNumber( value )
return value
end
return CalculatedCell