Автоматическое заполнение состояния из почтового индекса при поступлении записи формы из Google Forms в Google Sheets

#javascript #google-apps-script #google-sheets #zipcode

#javascript #google-apps-script #google-sheets #почтовый индекс

Вопрос:

У меня есть форма Google, которая запрашивает почтовый индекс пользователя. Я бы хотел, чтобы скрипт Google Sheet автоматически заполнял состояние в другом столбце на основе почтового индекса из записи. Я нашел этот скрипт на этом веб-сайте, но при его запуске вывод пустой.

Я не уверен, что нужно будет добавить в код, чтобы заставить его работать.

 function getState(input) {

  /* Ensure param is a string to prevent unpredictable parsing results */
  if (typeof input !== 'string') {
      console.log('Must pass the zipcode as a string.');
      return;
  }
 
  /* Ensure we have exactly 5 characters to parse */
  if (input.length !== 5) {
      console.log('Must pass a 5-digit zipcode.');
      return;
  } 

  /* Ensure we don't parse strings starting with 0 as octal values */
  const zipcode = parseInt(input, 10); 

  let st;
  let state;

  /* Code cases alphabetized by state */
  if (zipcode >= 35000 amp;amp; zipcode <= 36999) {
      st = 'AL';
      state = 'Alabama';
  } else if (zipcode >= 99500 amp;amp; zipcode <= 99999) {
      st = 'AK';
      state = 'Alaska';
  } else if (zipcode >= 85000 amp;amp; zipcode <= 86999) {
      st = 'AZ';
      state = 'Arizona';
  } else if (zipcode >= 71600 amp;amp; zipcode <= 72999) {
      st = 'AR';
      state = 'Arkansas';
  } else if (zipcode >= 90000 amp;amp; zipcode <= 96699) {
      st = 'CA';
      state = 'California';
  } else if (zipcode >= 80000 amp;amp; zipcode <= 81999) {
      st = 'CO';
      state = 'Colorado';
  } else if (zipcode >= 6000 amp;amp; zipcode <= 6999) {
      st = 'CT';
      state = 'Connecticut';
  } else if (zipcode >= 19700 amp;amp; zipcode <= 19999) {
      st = 'DE';
      state = 'Delaware';
  } else if (zipcode >= 32000 amp;amp; zipcode <= 34999) {
      st = 'FL';
      state = 'Florida';
  } else if (zipcode >= 30000 amp;amp; zipcode <= 31999) {
      st = 'GA';
      state = 'Georgia';
  } else if (zipcode >= 96700 amp;amp; zipcode <= 96999) {
      st = 'HI';
      state = 'Hawaii';
  } else if (zipcode >= 83200 amp;amp; zipcode <= 83999) {
      st = 'ID';
      state = 'Idaho';
  } else if (zipcode >= 60000 amp;amp; zipcode <= 62999) {
      st = 'IL';
      state = 'Illinois';
  } else if (zipcode >= 46000 amp;amp; zipcode <= 47999) {
      st = 'IN';
      state = 'Indiana';
  } else if (zipcode >= 50000 amp;amp; zipcode <= 52999) {
      st = 'IA';
      state = 'Iowa';
  } else if (zipcode >= 66000 amp;amp; zipcode <= 67999) {
      st = 'KS';
      state = 'Kansas';
  } else if (zipcode >= 40000 amp;amp; zipcode <= 42999) {
      st = 'KY';
      state = 'Kentucky';
  } else if (zipcode >= 70000 amp;amp; zipcode <= 71599) {
      st = 'LA';
      state = 'Louisiana';
  } else if (zipcode >= 3900 amp;amp; zipcode <= 4999) {
      st = 'ME';
      state = 'Maine';
  } else if (zipcode >= 20600 amp;amp; zipcode <= 21999) {
      st = 'MD';
      state = 'Maryland';
  } else if (zipcode >= 1000 amp;amp; zipcode <= 2799) {
      st = 'MA';
      state = 'Massachusetts';
  } else if (zipcode >= 48000 amp;amp; zipcode <= 49999) {
      st = 'MI';
      state = 'Michigan';
  } else if (zipcode >= 55000 amp;amp; zipcode <= 56999) {
      st = 'MN';
      state = 'Minnesota';
  } else if (zipcode >= 38600 amp;amp; zipcode <= 39999) {
      st = 'MS';
      state = 'Mississippi';
  } else if (zipcode >= 63000 amp;amp; zipcode <= 65999) {
      st = 'MO';
      state = 'Missouri';
  } else if (zipcode >= 59000 amp;amp; zipcode <= 59999) {
      st = 'MT';
      state = 'Montana';
  } else if (zipcode >= 27000 amp;amp; zipcode <= 28999) {
      st = 'NC';
      state = 'North Carolina';
  } else if (zipcode >= 58000 amp;amp; zipcode <= 58999) {
      st = 'ND';
      state = 'North Dakota';
  } else if (zipcode >= 68000 amp;amp; zipcode <= 69999) {
      st = 'NE';
      state = 'Nebraska';
  } else if (zipcode >= 88900 amp;amp; zipcode <= 89999) {
      st = 'NV';
      state = 'Nevada';
  } else if (zipcode >= 3000 amp;amp; zipcode <= 3899) {
      st = 'NH';
      state = 'New Hampshire';
  } else if (zipcode >= 7000 amp;amp; zipcode <= 8999) {
      st = 'NJ';
      state = 'New Jersey';
  } else if (zipcode >= 87000 amp;amp; zipcode <= 88499) {
      st = 'NM';
      state = 'New Mexico';
  } else if (zipcode >= 10000 amp;amp; zipcode <= 14999) {
      st = 'NY';
      state = 'New York';
  } else if (zipcode >= 43000 amp;amp; zipcode <= 45999) {
      st = 'OH';
      state = 'Ohio';
  } else if (zipcode >= 73000 amp;amp; zipcode <= 74999) {
      st = 'OK';
      state = 'Oklahoma';
  } else if (zipcode >= 97000 amp;amp; zipcode <= 97999) {
      st = 'OR';
      state = 'Oregon';
  } else if (zipcode >= 15000 amp;amp; zipcode <= 19699) {
      st = 'PA';
      state = 'Pennsylvania';
  } else if (zipcode >= 300 amp;amp; zipcode <= 999) {
      st = 'PR';
      state = 'Puerto Rico';
  } else if (zipcode >= 2800 amp;amp; zipcode <= 2999) {
      st = 'RI';
      state = 'Rhode Island';
  } else if (zipcode >= 29000 amp;amp; zipcode <= 29999) {
      st = 'SC';
      state = 'South Carolina';
  } else if (zipcode >= 57000 amp;amp; zipcode <= 57999) {
      st = 'SD';
      state = 'South Dakota';
  } else if (zipcode >= 37000 amp;amp; zipcode <= 38599) {
      st = 'TN';
      state = 'Tennessee';
  } else if ( (zipcode >= 75000 amp;amp; zipcode <= 79999) || (zipcode >= 88500 amp;amp; zipcode <= 88599) ) {
      st = 'TX';
      state = 'Texas';
  } else if (zipcode >= 84000 amp;amp; zipcode <= 84999) {
      st = 'UT';
      state = 'Utah';
  } else if (zipcode >= 5000 amp;amp; zipcode <= 5999) {
      st = 'VT';
      state = 'Vermont';
  } else if (zipcode >= 22000 amp;amp; zipcode <= 24699) {
      st = 'VA';
      state = 'Virgina';
  } else if (zipcode >= 20000 amp;amp; zipcode <= 20599) {
      st = 'DC';
      state = 'Washington DC';
  } else if (zipcode >= 98000 amp;amp; zipcode <= 99499) {
      st = 'WA';
      state = 'Washington';
  } else if (zipcode >= 24700 amp;amp; zipcode <= 26999) {
      st = 'WV';
      state = 'West Virginia';
  } else if (zipcode >= 53000 amp;amp; zipcode <= 54999) {
      st = 'WI';
      state = 'Wisconsin';
  } else if (zipcode >= 82000 amp;amp; zipcode <= 83199) {
      st = 'WY';
      state = 'Wyoming';
  } else {
      st = 'none';
      state = 'none';
      console.log('No state found matching', zipcode);
  }

  return state;
}  

Комментарии:

1. Могу ли я спросить вас о потоке детализации для воспроизведения ситуации when it is run, the output is blank. ?

Ответ №1:

Я поместил это в изолированную среду кода и подтвердил, что с этой функцией нет проблем. Я добавил дополнительный код о том, как получить значение из этой функции:

 function getState(input) {
    /* Ensure param is a string to prevent unpredictable parsing results */
    if (typeof input !== "string") {
      console.log("Must pass the zipcode as a string.");
      return;
    }

    /* Ensure we have exactly 5 characters to parse */
    if (input.length !== 5) {
      console.log("Must pass a 5-digit zipcode.");
      return;
    }

    /* Ensure we don't parse strings starting with 0 as octal values */
    const zipcode = parseInt(input, 10);

    let st;
    let state;

    /* Code cases alphabetized by state */
    if (zipcode >= 35000 amp;amp; zipcode <= 36999) {
      st = "AL";
      state = "Alabama";
    } else if (zipcode >= 99500 amp;amp; zipcode <= 99999) {
      st = "AK";
      state = "Alaska";
    } else if (zipcode >= 85000 amp;amp; zipcode <= 86999) {
      st = "AZ";
      state = "Arizona";
    } else if (zipcode >= 71600 amp;amp; zipcode <= 72999) {
      st = "AR";
      state = "Arkansas";
    } else if (zipcode >= 90000 amp;amp; zipcode <= 96699) {
      st = "CA";
      state = "California";
    } else if (zipcode >= 80000 amp;amp; zipcode <= 81999) {
      st = "CO";
      state = "Colorado";
    } else if (zipcode >= 6000 amp;amp; zipcode <= 6999) {
      st = "CT";
      state = "Connecticut";
    } else if (zipcode >= 19700 amp;amp; zipcode <= 19999) {
      st = "DE";
      state = "Delaware";
    } else if (zipcode >= 32000 amp;amp; zipcode <= 34999) {
      st = "FL";
      state = "Florida";
    } else if (zipcode >= 30000 amp;amp; zipcode <= 31999) {
      st = "GA";
      state = "Georgia";
    } else if (zipcode >= 96700 amp;amp; zipcode <= 96999) {
      st = "HI";
      state = "Hawaii";
    } else if (zipcode >= 83200 amp;amp; zipcode <= 83999) {
      st = "ID";
      state = "Idaho";
    } else if (zipcode >= 60000 amp;amp; zipcode <= 62999) {
      st = "IL";
      state = "Illinois";
    } else if (zipcode >= 46000 amp;amp; zipcode <= 47999) {
      st = "IN";
      state = "Indiana";
    } else if (zipcode >= 50000 amp;amp; zipcode <= 52999) {
      st = "IA";
      state = "Iowa";
    } else if (zipcode >= 66000 amp;amp; zipcode <= 67999) {
      st = "KS";
      state = "Kansas";
    } else if (zipcode >= 40000 amp;amp; zipcode <= 42999) {
      st = "KY";
      state = "Kentucky";
    } else if (zipcode >= 70000 amp;amp; zipcode <= 71599) {
      st = "LA";
      state = "Louisiana";
    } else if (zipcode >= 3900 amp;amp; zipcode <= 4999) {
      st = "ME";
      state = "Maine";
    } else if (zipcode >= 20600 amp;amp; zipcode <= 21999) {
      st = "MD";
      state = "Maryland";
    } else if (zipcode >= 1000 amp;amp; zipcode <= 2799) {
      st = "MA";
      state = "Massachusetts";
    } else if (zipcode >= 48000 amp;amp; zipcode <= 49999) {
      st = "MI";
      state = "Michigan";
    } else if (zipcode >= 55000 amp;amp; zipcode <= 56999) {
      st = "MN";
      state = "Minnesota";
    } else if (zipcode >= 38600 amp;amp; zipcode <= 39999) {
      st = "MS";
      state = "Mississippi";
    } else if (zipcode >= 63000 amp;amp; zipcode <= 65999) {
      st = "MO";
      state = "Missouri";
    } else if (zipcode >= 59000 amp;amp; zipcode <= 59999) {
      st = "MT";
      state = "Montana";
    } else if (zipcode >= 27000 amp;amp; zipcode <= 28999) {
      st = "NC";
      state = "North Carolina";
    } else if (zipcode >= 58000 amp;amp; zipcode <= 58999) {
      st = "ND";
      state = "North Dakota";
    } else if (zipcode >= 68000 amp;amp; zipcode <= 69999) {
      st = "NE";
      state = "Nebraska";
    } else if (zipcode >= 88900 amp;amp; zipcode <= 89999) {
      st = "NV";
      state = "Nevada";
    } else if (zipcode >= 3000 amp;amp; zipcode <= 3899) {
      st = "NH";
      state = "New Hampshire";
    } else if (zipcode >= 7000 amp;amp; zipcode <= 8999) {
      st = "NJ";
      state = "New Jersey";
    } else if (zipcode >= 87000 amp;amp; zipcode <= 88499) {
      st = "NM";
      state = "New Mexico";
    } else if (zipcode >= 10000 amp;amp; zipcode <= 14999) {
      st = "NY";
      state = "New York";
    } else if (zipcode >= 43000 amp;amp; zipcode <= 45999) {
      st = "OH";
      state = "Ohio";
    } else if (zipcode >= 73000 amp;amp; zipcode <= 74999) {
      st = "OK";
      state = "Oklahoma";
    } else if (zipcode >= 97000 amp;amp; zipcode <= 97999) {
      st = "OR";
      state = "Oregon";
    } else if (zipcode >= 15000 amp;amp; zipcode <= 19699) {
      st = "PA";
      state = "Pennsylvania";
    } else if (zipcode >= 300 amp;amp; zipcode <= 999) {
      st = "PR";
      state = "Puerto Rico";
    } else if (zipcode >= 2800 amp;amp; zipcode <= 2999) {
      st = "RI";
      state = "Rhode Island";
    } else if (zipcode >= 29000 amp;amp; zipcode <= 29999) {
      st = "SC";
      state = "South Carolina";
    } else if (zipcode >= 57000 amp;amp; zipcode <= 57999) {
      st = "SD";
      state = "South Dakota";
    } else if (zipcode >= 37000 amp;amp; zipcode <= 38599) {
      st = "TN";
      state = "Tennessee";
    } else if (
      (zipcode >= 75000 amp;amp; zipcode <= 79999) ||
      (zipcode >= 88500 amp;amp; zipcode <= 88599)
    ) {
      st = "TX";
      state = "Texas";
    } else if (zipcode >= 84000 amp;amp; zipcode <= 84999) {
      st = "UT";
      state = "Utah";
    } else if (zipcode >= 5000 amp;amp; zipcode <= 5999) {
      st = "VT";
      state = "Vermont";
    } else if (zipcode >= 22000 amp;amp; zipcode <= 24699) {
      st = "VA";
      state = "Virgina";
    } else if (zipcode >= 20000 amp;amp; zipcode <= 20599) {
      st = "DC";
      state = "Washington DC";
    } else if (zipcode >= 98000 amp;amp; zipcode <= 99499) {
      st = "WA";
      state = "Washington";
    } else if (zipcode >= 24700 amp;amp; zipcode <= 26999) {
      st = "WV";
      state = "West Virginia";
    } else if (zipcode >= 53000 amp;amp; zipcode <= 54999) {
      st = "WI";
      state = "Wisconsin";
    } else if (zipcode >= 82000 amp;amp; zipcode <= 83199) {
      st = "WY";
      state = "Wyoming";
    } else {
      st = "none";
      state = "none";
      console.log("No state found matching", zipcode);
    }

    return state;
}

const state = getState("05486");

console.log(state)