Skip to content

Instantly share code, notes, and snippets.

@timabell
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timabell/d42a4c7ff13adead6a0d to your computer and use it in GitHub Desktop.
Save timabell/d42a4c7ff13adead6a0d to your computer and use it in GitHub Desktop.
spreadsheet macro for making pretty sql insert blocks
'todo: handle empty properly, handle boolean true/false
' value - the value to turn into quoted and padded sql
' width - the largest item in this field, values will padded to fit.
' - use array formula =MAX(LEN(B290:B295)) to calculate this (ctrl-shift-enter to save an array formula)
' usage - apply this to all the cells you want to turn into sql, then concatenate the result in a final column
' like this =CONCATENATE(" (",P290,Q290,R290,S290,T290,U290,"),")
function SqlQuoter(value, width)
dim result as string
dim padding as integer
padding = 2 ' how many chars are needed to account for quotes etc
if value = 0 then 'fixme, should test for blank not zero
result = "null,"
padding = padding + 2
else
' cast to avoid flattening string concatenation
result = cstr(value)
' quote if needed
if isnumeric(value) then
result = result + ","
else
result = "'" + result + "',"
padding = padding + 2
endif
endif
'pad with trailing space to line up values across rows
result = result + string(width, " ") ' string will repeat a character n times
'trim the result to the right length
result = left(result, width + padding)
'return
SqlQuoter = result
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment