Skip to content

Instantly share code, notes, and snippets.

@Graham42
Created October 24, 2018 02:15
Show Gist options
  • Save Graham42/89f82d3adbfe77b76f246d869009f8df to your computer and use it in GitHub Desktop.
Save Graham42/89f82d3adbfe77b76f246d869009f8df to your computer and use it in GitHub Desktop.
Square Feet function for google sheets
/**
* @param {string} dimensions Ex. 12"3'x10"7'
* @customfunction
*/
function SQUAREFT(dimensions) {
var parts = dimensions.split("x")
var width = parseFeet(parts[0])
var height = parseFeet(parts[1])
return width * height
}
/**
* Given a ft and inches string return feet as integer
*/
function parseFeet(ftInches) {
var FEET = "'"
var INCHES = '"'
var FTIN = /^(\d+')?(\d+")?$/
if (!FTIN.test(ftInches)){
throw new Error("invalid measurement")
}
var matches = ftInches.match(FTIN)
var feet = matches[1] || (0 + FEET)
feet = parseInt(feet.substring(0, feet.length - 1))
var inches = matches[2] || (0 + INCHES)
inches = parseInt(inches.substring(0, inches.length - 1))
return (feet * 12 + inches) / 12
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment