Hello, I am someone who doesn’t know much about coding. I recently discovered n8n, and it seems like even someone like me can enjoy a smarter way of living, so I am studying hard.
The workflow I want to create is to receive two book titles and quotes from a Google Sheet on Telegram every day. On the first day, I want to receive the first and second rows, on the second day, the third and fourth rows, and so on.
I tried asking ChatGPT, but I couldn’t find a clear solution, so I am posting here. I would really appreciate your help.
// Google Sheets에서 받아온 데이터를 배열로 변환
const rows = $input.all(); // 모든 데이터를 가져옴
// 현재 Offset 값 가져오기 (Set 노드에서 저장된 값 활용, 없으면 0)
let offset = $('offset').first().json.offset || 0;
// Offset을 기준으로 3개만 선택
const selectedRows = rows.slice(offset, offset + 3);
// 다음 실행을 위한 Offset 증가 (마지막 행을 초과하면 0으로 초기화)
offset += 3;
if (offset >= rows.length) {
offset = 0; // 처음부터 다시 시작
}
// 결과 반환 (선택된 데이터 & 다음 Offset 값 저장)
return [{ selectedRows, offset }];
You might want to add two other columns in your google sheet with the the date of the posting in one column and an index to make sure your posting in the correct order.
Don’t forget to configure the date in the ISO 8601 format (YYYY-MM-DD)
Then in your code node, you can use the following :
const today = new Date().toISOString().split('T')[0]; // Get today's date in YYYY-MM-DD format
const results = $input.all()
.filter(item => item.json["Date"] === today) // Filter for today's date
.sort((a, b) => a.json["index"] - b.json["index"]) // Sort by index
.map(item => ({ json: { name: item.json["name"], contents: item.json["contents"] } })); // Keep only name and contents
return results;