Force field value to be added to the sheet as a string

In some situations, there may be numbers that have leading 0’s, such as zip codes. By default, these will be converted to numbers in Google Sheets and the leading 0’s will be removed.

This snippet casts the value to a string before it is sent to Google Sheets, which will preserve the leading 0’s.

Installation: 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ 2. Update the FORMID and FIELDID accordingly.

Code

Filename: gpgs-send-number-as-string.php

<?php
/**
 * Gravity Perks // GP Google Sheets // Force field value to be added to the sheet as a string
 *
 * In some situations, there may be numbers that have leading 0's, such as zip codes. By default, these will be converted
 * to numbers in Google Sheets and the leading 0's will be removed.
 *
 * This snippet casts the value to a string before it is sent to Google Sheets, which will preserve the leading 0's.
 *
 * Installation:
 *   1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *   2. Update the FORMID and FIELDID accordingly.
 */
add_filter( 'gpgs_row_value_FORMID_FIELDID', function( $value, $form_id, $field_id, $entry, $original_value ) {
	return (string) $original_value;
}, 10, 5 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.