การหา week number ใน Power Automate Desktop

by

ในบทความนี้เราจะหา week number โดยใช้วิธีของ ISO 8601 หรือที่รู้จักกันในชื่อ European week numbering system.

ระบบนี้จะหา week number โดยนับเริ่มที่วันจันทร์ถึงวันอาทิตย์

มี 2 วิธีที่สามารถหา week number คือ:

  1. ใช้ Python Script
  2. ใช้ Excel function

วิธีที่ 1 ใช้ Python Script

Get current date and time

ใช้คำสั่ง “Get current date and time” เพื่อหาวันที่ปัจจุบัน คำสั่งนี้จะเก็บค่าไว้ในตัวแปรชื่อ “CurrentDateTime”.

หาวันที่ปัจจุบัน

Run Python script

ใช้คำสั่ง “Run Python script” แล้ววางโค้ดด้านล่างใน parameter “Python script to run”

import datetime
week = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]
print(week)
วาง python script ใน parameter “Python script to run”

Result 1

ผลลัพธ์จากการใช้ Python script เพื่อหา week number

Completed Flow 1

Completed Flow สำหรับการใช้ Python script หา week number

วิธีที่ 2 ใช้ Excel function

Launch Excel

ใช้คำสั่ง “Launch Excel” เพื่อเปิด Blank Excel blank

เปิด Blank Excel

Write to Excel worksheet (Today)

เขียนสูตร TODAY() ใน cell A1 เพื่อหาวันที่ปัจจุบัน

เขียน Excel function TODAY() ใน Excel cell A1

Write to Excel worksheet (Week Number)

เขียนสูตร WEEKNUM() ใน cell B1 โดยใช้ system 2 เพื่อหา week number แบบระบบที่ 2 ของวันที่ปัจจุบัน

สามารถอ่านบทความที่อธิบายเกี่ยวกับการใช้ฟังก์ชั่น WEEKNUM ที่นี่.

เขียน Excel function WEEKNUM() ใน Excel cell B1

Read from Excel worksheet

อ่านค่าจากผลลัพธ์ที่ได้จากฟังก์ชั่น WEEKNUM() ใน Excel cell B1, แล้วเก็บไว้ในตัวแปรชื่อ “WeekNum” (ดูรูปด้านล่างประกอบ)

อ่านค่าใน Excel cell B1 ซึ่งได้จากการใช้ฟังก์ชั่น WEEKNUM()

Close Excel

ปิด Excel โดยไม่ต้อง save

ปิด Excel โดยไม่ต้อง save

Power Automate Desktop จะเปิด Excel, เขียน Excel function ตามรูปด้านล่าง แล้วจะดึงค่าจาก Excel cell B2 ซึ่งก็คือ week number ของวันที่ปัจจุบัน

Power Automate Desktop ดึงค่าจาก Excel cell B1 ซึ่งคำนวณค่า week number

Result 2

ผลลัพธ์จากการใช้ Excel function เพื่อหา week number

Completed Flow 2

Completed Flow สำหรับการใช้ Excel function หา week number

สามารถ copy source code ของบทความนี้ได้จาก link ด้านล่างแล้วนำไปวางที่ Workspace ของ Power Automate Desktop ได้เลย

Video

Did You Enjoy this Article?

Subscription to get new articles sent to your inbox each week for FREE

Question?

If you have any questions or feedback about this article please leave a message in the comments section below.


Comments

Leave a comment

Subscribe to get new articles sent to your inbox each week for FREE