Thep Excel

$itemIndex – ตำแหน่ง index ของ item ปัจจุบัน

$itemIndex คือตัวแปรที่บอกตำแหน่ง (index) ของ item ปัจจุบันที่กำลังประมวลผล เริ่มนับจาก 0 (zero-based) ใช้สำหรับติดตาม item ใน multi-item workflow, สร้าง running number, หรือทำ conditional logic ตามตำแหน่ง เช่น skip item แรกหรือทำอะไรพิเศษกับ item สุดท้าย

=$itemIndex

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
8/10

Difficulty
2/10

Usefulness
8/10

Syntax & Arguments

=$itemIndex

How it works

นับลำดับ item เพื่อสร้าง ID หรือ reference number

นับลำดับ item เพื่อสร้าง ID หรือ reference number

Skip หรือ process พิเศษสำหรับ item แรก/สุดท้าย

Skip หรือ process พิเศษสำหรับ item แรก/สุดท้าย

สร้าง progress indicator ในการประมวลผลข้อมูลจำนวนม…

สร้าง progress indicator ในการประมวลผลข้อมูลจำนวนมาก

Batch processing โดยแบ่ง items ตามตำแหน่ง (เช่น ทุ…

Batch processing โดยแบ่ง items ตามตำแหน่ง (เช่น ทุก 10 items)

Debug workflow ด้วยการ log ตำแหน่งของ item ที่เกิด…

Debug workflow ด้วยการ log ตำแหน่งของ item ที่เกิด error

Examples

ดูตำแหน่ง item ปัจจุบัน
{{ $itemIndex }}
แสดงตำแหน่งของ item ปัจจุบันที่กำลังถูกประมวลผล เป็น zero-based index ตามมาตรฐาน programming (item แรก = 0)
n8n Formula:

={{ $itemIndex }}

Result:

0 (สำหรับ item แรก), 1 (item ที่สอง), ...

สร้างหมายเลขลำดับแบบ 1-based สำหรับแสดงผล
{{ $itemIndex + 1 }}
เพิ่ม 1 เข้าไปเพื่อแปลงจาก zero-based (0,1,2…) เป็น one-based (1,2,3…) ที่คนทั่วไปเข้าใจง่ายกว่า ใช้เวลาต้องการแสดงลำดับให้ user เห็น เช่น "รายการที่ 1", "รายการที่ 2"
n8n Formula:

={{ $itemIndex + 1 }}

Result:

1 (สำหรับ item แรก), 2 (item ที่สอง), ...

ทำ conditional logic สำหรับ item แรก
{{ $itemIndex === 0 ? 'Header' : 'Data' }}
ใช้ ternary operator เพื่อจัดการ item แรกแตกต่างจาก items อื่น เช่น item แรกเป็น header row หรือต้องการ skip item แรก pattern นี้ใช้บ่อยมากใน data processing ที่แถวแรกเป็น column headers
n8n Formula:

{{ $itemIndex === 0 ? 'Header' : 'Data' }}

Result:

"Header" (item แรก), "Data" (item อื่นๆ)

สร้าง progress indicator
{{ 'Processing item ' + ($itemIndex + 1) + ' of ' + $input.all().length }}
ผสม $itemIndex กับ $input.all().length เพื่อแสดงความคืบหน้าการประมวลผล ใช้ใน logging หรือ notification เพื่อให้รู้ว่า workflow กำลังทำงานถึงไหนแล้ว
n8n Formula:

={{ 'Processing item ' + ($itemIndex + 1) + ' of ' + $input.all().length }}

Result:

"Processing item 3 of 10"

FAQs

ทำไม item แรกเป็น 0 ไม่ใช่ 1?

เพราะ n8n ใช้ zero-based indexing ตามมาตรฐาน JavaScript และ programming ทั่วไป ถ้าต้องการแสดงผลเป็นเลข 1,2,3… ให้บวก 1 เข้าไป เช่น {{ $itemIndex + 1 }} ครับ

ใช้ใน Code node ได้ไหม?

ไม่ได้โดยตรงครับ $itemIndex ใช้ได้เฉพาะใน expressions เท่านั้น ใน Code node ต้องใช้วิธีอื่น เช่น loop ด้วย forEach พร้อม index parameter หรือเพิ่มฟิลด์ index ใน Set node ก่อนส่งเข้า Code node

ใช้ใน Loop Over Items node ยังไง?

ใน Loop Over Items node ให้ใช้ {{$node[“Loop Over Items”].context[“currentRunIndex”]}} แทนเพื่อดู run index ของ loop ส่วน $itemIndex จะเป็น index ภายใน batch ของแต่ละ run ครับ

Resources & Related

Additional Notes

$itemIndex เป็นตัวแปร built-in ที่เก็บ index ของ item ปัจจุบันที่กำลังถูกประมวลผล เริ่มนับจาก 0 ตามมาตรฐาน programming ใช้ได้ใน expressions ทุกที่ เช่น Set node, IF node, และ HTTP Request parameters

ที่เจ๋งคือ $itemIndex ทำให้เราสามารถทำ conditional logic ตามตำแหน่งได้ทันที เช่น ถ้าเป็น item แรก ($itemIndex === 0) ให้ทำอะไรพิเศษ หรือใช้ modulo เพื่อทำ batch processing ทุก N items ครับ

ส่วนตัวผมใช้ $itemIndex บ่อยมากในการสร้าง running number และ progress indicator โดยรวมกับ $input.all().length เพื่อแสดง “กำลังประมวลผล item 3 จาก 10” ครับ 😎

Leave a Reply

Your email address will not be published. Required fields are marked *