Pick Random Row from Random Google Sheets

How to Pick data from Random Google Sheets. Each Sheet Contains a Different Row of Sentence.

Is it possible to fetch data from the Random Google Spreadsheet and Get a random Row from that using the function node?

If Randomizer is there I can use it, But with Function node is it possible?

You can use the same node as in this topic:

The only thing you have to do before it is to merge all the data of all the sheets before with multiple Merge-Nodes set to “append”.

1 Like

Actually I want to Read from the Sheets. Do they still work?

And I have to use Merge Node? It has only two input connectors.

Yes, you need multiple Merge-Nodes. You merge the first two Sheets, then the result of that two with the third, and so on. In the last node you will then have the data of all the Sheets.

1 Like

I tried this with Two Google Sheets and It works like a charm.

But It’ll fetch data from Two sheets. It’ll increase the quota used. I’m asking like. Is there any way we can make it to pick from Only one Sheet at a time?

I tried with Two Function Nodes too.

Still, it’s picking the sheet randomly. Pinging Both.

Do you want to get data from totally different documents or for different sheets in the same document?

1 Like

I want to get it from totally different documents.

The alternative which requires some custom code but then at least does then only one request to Google sheets would be to randomly select a document and then only select its data. You can then get a random quote of that document with the same code as you currently have in the “Get Random Row” code:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const documents = [\n  '1234',\n  '5678',\n];\n\nitems[0].json.documentId = documents[Math.floor(Math.random()*documents.length)];\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        584,
        459
      ]
    },
    {
      "parameters": {
        "sheetId": "={{$json[\"documentId\"]}}",
        "options": {}
      },
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        784,
        459
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

You should however make sure that you do not have a document which has a very low number of entries because the chance that you will get the same result regularly would be very high.

1 Like

Got it. Can we pick the Credentials in the same way? Incase of Document from different account?

No that is not possible.

Oh my bad.

Please consider adding Randomizer Node in your ideas (Roadmap). It’ll change the whole game.