Sure, with pleasure.
Here is the updated code.
First of all, the “FindIndex” error has been resolved.
(I can’t say exactly what the issue was, though — Qwen identified and fixed it.)
There are now some additional debug functions included.
Most importantly, the script now also checks whether an entire category has already been fully written. If so, it will be skipped.
Furthermore, the cell reference in which the “ok” needs to be placed (once the article has been written) is now being generated — including the full range string that is required as a variable in the “Update Google Sheets” node to dynamically update the cell (“sheetname!cell”).
The variable “Google Update Value”, which is also defined in this code, is not being used. I have handled this manually for now.
There is currently another issue: the Google Update node always deletes the content of the cell to the right of the cell that is supposed to be updated.
Because the node requires content under “spreadsheet content”, which assumes a comma-separated value (in my case “ok,”), a second (empty) content item is interpreted automatically — and this overwrites the second cell.
So it seems that it is not possible to update just a single cell.
Qwen suggested a few possible solutions, but none of them worked. That’s why there’s a value included in the code that was intended to be used as a variable.
However, I have now resolved this manually.
I’ve set the content to “ok,{{request}}”. This enters “ok” into the first cell, and the second cell — which actually should remain untouched — is now overwritten with the same value held in the variable.
So it works now, but it’s just a workaround.
If there is any info on how to properly update just a single cell, I’d be very happy to hear it. Maybe it’s a bug, or maybe it’s simply not possible in the node at the moment.
Here is the code:
// --- Dynamic selection of the first unmarked title ---
const sheetData = ai.vars.content_websites;
const sheetName = "test"; // ⚠️ Replace with your sheet name!
if (!sheetData || !sheetData.values || !Array.isArray(sheetData.values)) {
console.error("Error: Invalid sheet data", sheetData);
ai.vars.request = null;
ai.vars.choosenkat = null;
ai.vars.tabzeile = null;
ai.vars.zelle = null;
ai.vars.google_update_range = null;
return;
}
const rows = sheetData.values;
// 1. Find all categories that have at least one unmarked title (A ≠ "ok")
const categoriesWithPending = [...new Set(
rows
.slice(1)
.filter(row => {
if (!Array.isArray(row) || row.length < 3) return false;
const status = (row[0] || "").toString().trim();
const category = (row[2] || "").toString().trim();
return category && status !== "ok";
})
.map(row => row[2].toString().trim())
)];
// 2. No pending titles?
if (categoriesWithPending.length === 0) {
console.warn("All titles have already been processed.");
ai.vars.choosenkat = null;
ai.vars.request = null;
ai.vars.tabzeile = null;
ai.vars.zelle = null;
ai.vars.google_update_range = null;
return;
}
// 3. Select a random category
const chosenCategory = categoriesWithPending[Math.floor(Math.random() * categoriesWithPending.length)];
ai.vars.choosenkat = chosenCategory;
// 4. Find the first unmarked title in this category
const matchingRowIndex = rows.findIndex((row, index) => {
if (index === 0) return false; // Skip header
if (!Array.isArray(row) || row.length < 3) return false;
const status = (row[0] || "").toString().trim();
const category = (row[2] || "").toString().trim();
return category === chosenCategory && status !== "ok";
});
// 5. Save result
if (matchingRowIndex !== -1) {
ai.vars.request = rows[matchingRowIndex][1]; // Title
ai.vars.tabzeile = matchingRowIndex + 1; // Row number (1-based)
ai.vars.zelle = `A${matchingRowIndex + 1}`; // Cell A + row
ai.vars.google_update_range = `${sheetName}!${ai.vars.zelle}`; // e.g. "test!A24"
console.log("✅ Title:", ai.vars.request);
console.log("📝 Cell to be marked:", ai.vars.google_update_range);
} else {
console.error("❌ No title found – logic error.");
ai.vars.request = null;
ai.vars.tabzeile = null;
ai.vars.zelle = null;
ai.vars.google_update_range = null;
}
// Debug output
console.log("Cell:", ai.vars.zelle);
console.log("Range:", ai.vars.google_update_range);
console.log("Category:", ai.vars.choosenkat);
console.log("Title:", ai.vars.request);
// Set values for Google Sheets update
ai.vars.google_update_values = [["ok"]];
ai.vars.google_update_range = `${sheetName}!${ai.vars.zelle}`; // e.g. test!A9
// Debug output
console.log("📤 google_update_values (Type):", typeof ai.vars.google_update_values);
console.log("📋 Content:", ai.vars.google_update_values);