Saturday, January 18, 2025
API KEY
HomeGoogle Apps Scriptวิธีดึงข้อมูลใน Array ออกมาใช้งาน

วิธีดึงข้อมูลใน Array ออกมาใช้งาน

วิธีดึงข้อมูลใน Array ออกมาใช้งานด้วย Google App Script

1.Array 1 มิติ

      function showArrayValues() {
        // สร้าง array ตัวอย่าง
        var myArray = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'];
        var message = ""
      
        // วนลูปผ่าน array และแสดงค่าทั้งหมด
        for (var i = 0; i < myArray.length; i++) {
          message += myArray[i] + "\n"
        }
        Logger.log(message);
      }

      2. Array 2 มิติ

      function extractData() {
        // Sample array
        var myArray = [
          ["Name", "Age", "City"],
          ["John", 25, "New York"],
          ["Jane", 30, "San Francisco"],
          ["Mike", 22, "Chicago"]
        ];
      
        // Extracting specific data
        var names = [];
        for (var i = 1; i < myArray.length; i++) {
          names.push(myArray[i][0]); // Extracts the 'Name' from each row
        }
        Logger.log(names); // Logs ['John', 'Jane', 'Mike']
      }

      3. Array 3 มิติ

      function extractData3D() {
        // Sample 3D array
        var myArray3D = [
          [ // First 2D Array
            ["Name", "Age", "City"],
            ["John", 25, "New York"],
            ["Jane", 30, "San Francisco"],
            ["Mike", 22, "Chicago"]
          ],
          [ // Second 2D Array
            ["Name", "Age", "City"],
            ["Anna", 28, "Miami"],
            ["Tom", 33, "Boston"],
            ["Lisa", 24, "Seattle"]
          ]
        ];
      
        // Extracting specific data from each 2D array
        var names = [];
        for (var i = 0; i < myArray3D.length; i++) { 
          for (var j = 1; j < myArray3D[i].length; j++) {
            names.push(myArray3D[i][j][0]); // Extracts the 'Name' from each row in each 2D array
          }
        }
        Logger.log(names); // Logs ['John', 'Jane', 'Mike', 'Anna', 'Tom', 'Lisa']
      }
      RELATED ARTICLES

      LEAVE A REPLY

      Please enter your comment!
      Please enter your name here

      - Advertisment -
      คอร์สเรียนออนไลน์

      Most Popular

      Recent Comments