SHEET_UPDATERECORD::match_range( mixed $existing_rows, mixed $sheet, mixed $selected_column_range, mixed $lookup_field_value )

match_range


Description Description

Look for the range that matches the value.


Top ↑

Parameters Parameters

$existing_rows

(Required)

$sheet

(Required)

$selected_column_range

(Required)

$lookup_field_value

(Required)


Top ↑

Return Return

(void)


Source Source

File: src/integrations/google-sheet/actions/sheet-updaterecord.php

	public function match_range( $existing_rows, $sheet, $selected_column_range, $lookup_field_value ) {

		$done_lookup   = false;
		$matched_range = false;
		$i             = 0;

		foreach ( $existing_rows as $range => $sheet_rows ) {
			$j = 1;
			foreach ( $sheet_rows as $existing_row_value ) {
				$value = array_shift( $existing_row_value );
				// The field value that we are matching against.

				if ( $lookup_field_value === $value ) {
					$done_lookup = true;
					break;
				}
				$j ++;
			}
			$i ++;
			if ( $done_lookup ) {
				// Range starts at "A".
				$matched_range = $sheet . '!A' . ( $j + 1 );
				break;
			}
		}

		if ( empty( $matched_range ) ) {
			// Complete with error if no cell value matches the lookup value.
			$error_message = sprintf(
				esc_html__( "Notice: No cell values matches with '%1\$s' under '%2\$s' column in Sheet: '%3\$s'.", 'uncanny-automator' ),
				$lookup_field_value,
				$selected_column_range,
				$sheet
			);

			throw new \Exception( $error_message );
		}

		return $matched_range;
	}