Home > Blockchain >  Format Text using App Script in Google sheet
Format Text using App Script in Google sheet

Time:01-29

Using the App Script, I need to convert text:

Input:

POLYGON ((-111.705934405 32.76300316, -111.706191897 32.748205765, -111.706191897 32.748205765, -111.706191897 32.748205765, -111.688682437 32.748566707, -111.688939929 32.733261511, -111.655036807 32.732828307, -111.655294299 32.740914437, -111.645938754 32.740842243, -111.646024585 32.748783271, -111.654607654 32.74914421, -111.654092669 32.763291866, -111.705934405 32.76300316))

Output:

[[{"lat":32.763597994888265,"lng":-111.70779863788822},{"lat":32.763670170794725,"lng":-111.65269533588626},{"lat":32.74887288656241,"lng":-111.65346781208255},{"lat":32.74880069866226,"lng":-111.64479891254642},{"lat":32.741004061134866,"lng":-111.64548555805423},{"lat":32.741004061134866,"lng":-111.65424028827884},{"lat":32.73356774344866,"lng":-111.65466944172123},{"lat":32.732990139407825,"lng":-111.70608202411869}]]

Using App script. I have converted text using formulas. Here is That sheet. Polygon can have 200 points. It is difficult to adjust those points in the formula.

And note that the latitude and longitude is reversed in the input.

CodePudding user response:

In your situation, how about the following sample script?

Sample script:

const value = "POLYGON ((-111.705934405 32.76300316, -111.706191897 32.748205765, -111.706191897 32.748205765, -111.706191897 32.748205765, -111.688682437 32.748566707, -111.688939929 32.733261511, -111.655036807 32.732828307, -111.655294299 32.740914437, -111.645938754 32.740842243, -111.646024585 32.748783271, -111.654607654 32.74914421, -111.654092669 32.763291866, -111.705934405 32.76300316))";
const r1 = value.match(/\(\((.*)\)\)/);
const r2 = r1[1].split(",").map(e => {
  const [lng, lat] = e.trim().split(" ");
  return { lat: Number(lat), lng: Number(lng) };
});
const res = [r2];
console.log(res);

  • When this script is run, the following result is obtained.

      [[{"lat":32.76300316,"lng":-111.705934405},{"lat":32.748205765,"lng":-111.706191897},{"lat":32.748205765,"lng":-111.706191897},{"lat":32.748205765,"lng":-111.706191897},{"lat":32.748566707,"lng":-111.688682437},{"lat":32.733261511,"lng":-111.688939929},{"lat":32.732828307,"lng":-111.655036807},{"lat":32.740914437,"lng":-111.655294299},{"lat":32.740842243,"lng":-111.645938754},{"lat":32.748783271,"lng":-111.646024585},{"lat":32.74914421,"lng":-111.654607654},{"lat":32.763291866,"lng":-111.654092669},{"lat":32.76300316,"lng":-111.705934405}]]
    

Note:

  • If you want to retrieve the values from column "A" of your sample Spreadsheet, how about the following sample script? In this case, from your showing the expected value, each element of the result array is a 2-dimensional array. Please be careful about this.

    function myFunction() {
      const sheetName = "Sheet1";
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      const values = sheet.getRange("A2:A"   sheet.getLastRow()).getValues();
      const res = values.map(([a]) => {
        const r1 = a.match(/\(\((.*)\)\)/);
        const r2 = r1[1].split(",").map(e => {
          const [lng, lat] = e.trim().split(" ");
          return { lat: Number(lat), lng: Number(lng) };
        });
        return [r2];
      });
      console.log(res);
    }
    

Reference:

  • Related