Utilizing setInterval to navigate through a Google Sheets document

I have a Google spreadsheet with 4 rows, and I want to iterate through them one at a time every hour. The current code works but posts all four rows at the same time. How can I post one row every hour continuously?

require('console-stamp')(console, {
    pattern: 'dd/mm/yyyy HH:MM:ss.l'
});

var Twit = require('twit');
var config = require('./dmconfig');
var Tabletop = require('tabletop');

var bot = new Twit(config);
var spreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1234567/edit?usp=sharing';

var link = () => {
    Tabletop.init({
        key: spreadsheetUrl,
        callback(data, tabletop) {
            data.forEach(d => {
                var status = d.tweetMsg + ' ' + Math.floor(Math.random() * 10000) + ' ' + d.userURL + ' via @AlDerbyshireG';
                console.log(status)
                bot.post('statuses/update', {
                    status
                },
                (err, response, data) => {
                    if (err) { console.log(err) }
                    else { console.log('Post success!') }
                });
            });
        },
        simpleSheet: true
    });
}

link()
setInterval(link, 1000 * 60 * 60)

Answer №1

According to the TableTop documentation found in the repository's readme, setting simpleSheet: true will provide your callback function with an array of rows from the first sheet of the workbook.

Here is the sample code:

callback: (data, tabletop) => {
  data.forEach(d =>

This code processes every row on that particular sheet.

If you only want to call a function for a single row each time the callback is executed, you need to create another variable within the scope - let's name it rowIndex. This variable should be initialized when your bot server loads and then updated and checked against limits within the callback.

var rowIndex = 0;

var link = () => {
    Tabletop.init({
        key: spreadsheetUrl,
        callback(data, tabletop) {
            if (!data.length) { return; } // Ensure the spreadsheet isn't empty
            // Account for added or removed messages since last action.
            rowIndex = rowIndex % data.length;
            var d = data[rowIndex];
            /**
             * Code to execute for this specific row `d`
             */
            // Update the state variable for future calls to target different rows.
            ++rowIndex;
        }, // End definition of callback
        simpleSheet: true
   });
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Introducing the innovative Icon Collapsable JQuery - a versatile tool that

I'm looking to set custom icons for the JQuery Collapsable view without having to make changes to the default JQuery CSS and .JS files. You can take a look at my starting point on jsFiddle here: http://jsfiddle.net/jakechasan/M7LLU/ Although I' ...

Adding content to an Element Id using JavaScript can be done by utilizing the .insertAfter or .append methods. Since it involves a script tag, you cannot use the dollar sign

I need to include a script after a specific div in my HTML code. Here is what I currently have: <div id="uno">insert after this</div><br /> <p>this is a paragraph</p><br /> <div>this is a div</div> var mysc ...

The ElementNotVisibleException error was triggered because the element is currently hidden from view, making it unable to be interacted with through the command

I'm facing an issue when trying to clear the text box. The error message I am receiving is: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.14 se ...

Tips for adding a "dynamic" backdrop in THREE.js

I stumbled upon this website: Notice how the gradient background changes with a 360 degree shading effect as you move the camera. Which aspect of THREE.js would be responsible for creating something like this? Could someone provide guidance on where to ...

Integrating secondary functionality into fullPage and Vue.js

I am encountering an error in VueJS that says app.js:11754Uncaught TypeError: (intermediate value)(intermediate value).bind is not a function. I want to trigger the isLoaded function inside fullPage. I have tried using 'this' binding but it' ...

Access the data within a jsonArray using Cypress

I'm dealing with a test.json file that contains a jsonArray [{ "EMAIL": "email_1", "FIRST_NAME": "Daniel" }, [{ "EMAIL": "email_2", "FIRST_NAME": "John" }] ] I'm trying to figure out how to use cypre ...

Postman encountered a preflight failure on the Node API request

After calling the API from a browser, it successfully returns a correct response from the server: { "error": false, "message": "Hello World" }. However, when the same request is made using Postman, it results in an unexpected HTML content prompting to ena ...

Ways to retrieve the identifier of the uploaded document in GridFs for linking with another model

After creating a GridFS and uploading an image using the code snippet below: app.post("/upload", upload.single("photo"), function(req, res) { res.redirect("/images"); }) I also have a separate model for users: var userSchema = new mongoose.Schema({ ...

Achieving this result in HTML using a jQuery accordion component

Hey there, I'm looking to create a full-height accordion that extends from the bottom to the top of the page, similar to what you see on this website: . I also have a footer positioned below the accordion. The content should load when the page loads ...

The Bootstrap modal is not properly clearing all attribute properties when it is hidden

Alrighty, so I've got this modal set up: <div id="modal-container" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content center-block" style="displ ...

What is the best way to utilize the react hook useEffect just once in my scenario?

After looking into it, I realized that my question may seem like a duplicate at first glance, but upon further inspection, I found that it is not. I have come across many questions with the same title but different cases. The issue I am facing is that I h ...

Dynamic fade effect with GSAP on scroll

Currently, I am attempting to implement a fade out animation with GSAP Scroll Trigger. The aim is for the page to first scroll across the X axis of the title before scrolling up and fading out. While I have made some progress, I am facing an issue where th ...

Building a straightforward expandable/collapsible tree view with AngularJS by utilizing a particular data format

Seeking a tree view control that displays as follows: http://plnkr.co/edit/JAIyolmqPqO9KsynSiZp?p=preview +Parent child +Parent child child child child +Parent child child child child child chil ...

Error: The function m.easing[this.easing] is not defined

I have been working on creating anchor link scrolling and tooltip display using bootstrap, but I am encountering an issue. $(window).scroll(function(){ if ($(window).scrollTop() >= 100) { $('#header').addClass('fixed'); ...

The onfocus event in Chrome's JavaScript add-on fails to trigger

A few months back, I encountered an issue where onFocus events added using JavaScript were not firing only in Chrome (my version Version 103.0.5060.114 (Official Build) (x86_64) OSX 12.4). Interestingly, they worked fine in iOS Chrome, other browsers, an ...

html5-jquery checkbox change event not firing

I successfully implemented an on/off switch using the method outlined in for HTML5. The functionality of the switch itself is working perfectly - click on it to toggle between states and query the checked state of the input/checkbox. However, I am facing ...

What is the best way to transfer an object between views in Django?

Background/Issue In my web application, I have a job that involves running a python script to log into LinkedIn. This script launches headless chromium, navigates to the LinkedIn login page, and logs in with the proper credentials. However, sometimes Link ...

Arrange search results using $in array parameter in MongoDB

My challenge involves managing two collections: users and items. Within the user.profile.savedItems array, items are saved in the following format: {"itemId" : "yHud5CWpdPaEc6bdc", "added" : ISODate("2014-09-12T22:28:11.738Z")} My goal is to retrieve ...

Looking for assistance in setting a new initial state for a javascript toggle on page load

Need assistance with customizing the Youtube-TV JS plugin for a client website? The current setup loads the player with a playlist in an open state, but the requirement is to load it with the toggle closed, displaying the array of playlists instead. If y ...

Choose a paragraph of text that spans multiple lines without exceeding the width

In the image above, the red selection crosses outside of the yellow area when selecting. I would like to only select within the yellow part as shown below: Here is the HTML: <div id="parent"> <div id="child"> This is some content. ...