💻
Code Snippet
  • Overview
  • General
    • Anaconda
  • GUI
    • PyQT
      • Qt Design
  • Pandas
    • Read Data
    • Replace
  • Articles
    • Python. PyQt
    • Offline Payment Wallet With Django
    • Documentation Encrypt File
    • Play With Pillow
  • Fontend
    • Snippet
    • Hugo
    • JavaScript
      • Form Validation
  • Finance
    • Library
      • yfinance
  • Notebook
    • Untitled
    • Snippet
  • Python
    • Download file
    • Date and Time
    • Snippet
    • Compile .exe
    • Overview
    • Google
      • Samples for Google Workspace
      • Drive
      • GoogleSheet
    • Virtual environment
    • Database
      • Pickle()
    • Datatypes
      • Excel
      • Dictionary
        • xmltodict()
    • File Handling
      • shutil()
      • Get the File Name
      • Get the Full Path
      • Check the File Size
      • Get File Creation Date
      • Find All File
        • Untitled
    • Dictionary
      • Convert Two Lists
  • Data Science
    • HTTP requests
  • Google Workspace
    • Overview
    • Apps Script
      • ์Note
      • Overview
      • Snippet
        • HTML Service
        • Fetch API
      • Quickstart
      • Google Sheets
        • Overview
          • Snippet
        • Fundamentals
          • Macros & Custom Functions
          • Spreadsheets, Sheets, and Ranges
          • Working with Data
          • Data Formatting
          • Chart and Present Data
        • Built-in Google Services
        • Fetch and format API data
        • Connected Sheets
  • Git
  • Mini Lab
    • Line
    • Python
  • Function
    • Python
      • Date&Time
  • Database
    • SQLite
      • Example
Powered by GitBook
On this page
  • Document : https://developers.google.com/apps-script/reference
  • การใช้งาน Custom Function
  • our first script

Was this helpful?

  1. Google Workspace
  2. Apps Script

Overview

Previous์NoteNextSnippet

Last updated 3 years ago

Was this helpful?

Document :

คือวิธีการสั่งงาน Google Apps โดยการเขียนโปรแกรม (ใช้ฐานจาก java script) ซึ่งการเขียน Google Apps Script ช่วยให้เราสามารถใช้งาน Google Apps ในรูปแบบใหม่ๆ ได้หลากหลายมากขึ้น เช่น สั่งให้ส่งอีเมลตามเวลาที่ตั้งไว้ สั่งให้คำนวณค่าใน spreadsheet ตามเงื่อนไขที่กำหนด (โดยอัตโนมัติ) ส่วนบทความนี้จะแนะนำการใช้งาน Google Apps Script เพื่อทำ custom function ใน Google Sheets ครับ

Custom Function คืออะไร? โดยทั่วไป การใช้งานโปรแกรมประเภท spreadsheet ซึ่งมีลักษณะเป็นตาราง 2 มิติ มักจะไม่ใช่แค่การเก็บข้อมูลอย่างเดียว เราสามารถใส่การคำนวณให้กับ cell ต่างๆ ใน spreadsheet ได้ เช่น หาค่ารวม (SUM) ค่าเฉลี่ย (AVG) และอื่นๆ อีกมากมาย

การคำนวณค่า เราจะใช้การเรียก function (หรือที่เรียกกันว่า "สูตร") ที่ spreadsheet เตรียมไว้ให้ โดยทั่วไปจะเป็นการใส่เครื่องหมายเท่ากับ (=) แล้วตามด้วยชื่อ function ลงใน cell ปลายทาง เช่น ที่ cell A6 หากเราใส่ค่า =AVG(A1:A5) หมายถึงให้โปรแกรมแสดงค่าเฉลี่ยของค่าใน cell A1 ถึง A5 และแสดงผลลัพธ์ที่ A6 สำหรับการทำงานทั่วไป เราสามารถใช้ function ที่มีอยู่ในการทำงานได้ทันที (ดูรายการ function ทั้งหมดของ Google spreadheets ได้ที่นี่) แต่ในบางกรณี หากการทำงาน (หรือการคำนวณ) นั้นๆ ไม่มี function มารองรับ (เนื่องจากเป็นวิธีการที่เฉพาะทางหรืออะไรก็แล้วแต่) เราสามารถเขียน function ขึ้นมาเพื่อใช้งานเองได้ และ function ที่เขียนขึ้นมาก็เรียกว่า custom function นั่นเอง การเขียน Custom Function การเขียน function เพื่อใช้งานเองนั้น เป็นรูปแบบหนึ่งในการใช้งาน Google Apps Script (ซึ่่ง Google Apps Scirpt สามารถใช้งานได้หลากหลายรูปแบบ) และเป็นวิธีการที่ใช้งานได้กับ spreadsheet เท่านั้น ขั้นแรกให้เปิด Script Editor จาก Google Sheets ได้จากเมนู Tools > Script editor...

ตั้งชื่อ function ที่ต้องการ (ชื่อนี้จะเป็นชื่อที่เราเรียกใช้จาก spreadsheet) หาก function ที่เราจะเขียนมานั้น ต้องมี input ก็ให้ระบุ input ตอนประกาศ function ด้วย

เขียน Google Apps Script เพื่อคำนวณค่าที่ได้รับ และ return ผลลัพธ์คืน

การใช้งาน Custom Function

หลังจากเขียน Custom Function ใน script editor เรียบร้อยแล้ว ในหน้า spreadsheet เราสามารถเรียกใช้งาน custom function ได้ด้วยวิธีการเดียวกับการเรียก function ทั่วไป นั่นคือใส่เครื่องหมายเท่ากับ (=) แล้วตามด้วยชื่อ function ตามที่ตั้งไว้ใน Script Editor (หาก custom function มีค่าที่ต้องใช้ในการคำนวณ ก็ให้ใส่ค่าหรืออ้างอิง cell ต้นทางด้วย) ก็จะได้ผลลัพธ์ของ custom function ใน cell ปลายทาง (cell ที่ใส่สูตร)

=Bahttext(arg) for Google Sheets ตัวอย่างการใช้ Custom Function

custom function ที่ผู้เขียนทดลองเขียนขึ้นมาใช้งานส่วนตัวนั้น มาจากสูตร =Bahttext(number) ของโปรแกรม Excel (ไม่แน่ใจเรื่องลิขสิทธิ์การตั้งชื่อสูตรนะครับ หากใครต้องการท้วงติงรบกวนแจ้งมาที่ pakorn.n@tangerine.co.th ได้เลยครับ) โดยหน้าที่คือสูตรนี้จะรับค่าเป็นตัวเลขและให้ผลลัพธ์เป็นตัวหนังสือไทยตามมูลค่าของตัวเลขที่ระบุไว้

our first script

Set it up

To build the script, follow the steps below.

templates/standalone/helloWorld.gs
/**
 * Creates a Google Doc and sends an email to the current user with a link to the doc.
 */
function createAndSendDocument() {
  // Create a new Google Doc named 'Hello, world!'
  var doc = DocumentApp.create('Hello, world!');

  // Access the body of the document, then add a paragraph.
  doc.getBody().appendParagraph('This document was created by Google Apps Script.');

  // Get the URL of the document.
  var url = doc.getUrl();

  // Get the email address of the active user - that's you.
  var email = Session.getActiveUser().getEmail();

  // Get the name of the document to use as an email subject line.
  var subject = doc.getName();

  // Append a new string to the "url" variable to use as an email body.
  var body = 'Link to your doc: ' + url;

  // Send yourself an email with a link to the document.
  GmailApp.sendEmail(email, subject, body);
}
ตั้งชื่อ function พร้อมค่า input ที่รับมาคำนวณ
เมื่อคำนวณเสร็จ ให้คืนค่าผลลัพธ์
ตัวอย่างการเรียกใช้ =Bahttext(A1) ซึ่งเป็น custom fuction

Build and run a simple that creates a Google Doc and emails you a link to it.

standalone script
https://developers.google.com/apps-script/reference
Google Apps Script