首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用setInterval遍历谷歌电子表格

使用setInterval遍历谷歌电子表格
EN

Stack Overflow用户
提问于 2018-06-02 01:13:57
回答 1查看 131关注 0票数 0

我有4行在谷歌电子表格,并希望迭代通过他们每1个小时一次。这段代码目前可以工作,但同时发布了所有这四个代码。我如何才能每小时1个小时,并使其连续?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 07:25:04

根据TableTop文档(字面意思是在readme of the repo中),使用simpleSheet: true可以向回调函数提供工作簿第一个工作表中的行数组。

你的代码:

代码语言:javascript
复制
callback: (data, tabletop) => {
  data.forEach(d =>

从而指示要处理该工作表上的每一行。

要在每次执行callback时只调用一行的函数,您需要定义其他一些限定了作用域的变量--让我们称其为rowIndex。您应该在加载bot服务器时初始化此变量,然后在callback中对其进行递增和边界检查。

代码语言:javascript
复制
var rowIndex = 0;

var link = () => {
    Tabletop.init({
        key: spreadsheetUrl,
        callback(data, tabletop) {
            if (!data.length) { return; } // Guard against empty spreadsheet
            // Wrap around here, in case new messages were added or deleted since the last invocation.
            rowIndex = rowIndex % data.length;
            var d = data[rowIndex];
            /**
             * Code to call for just this array element `d` (read: row)
             */
            // Increment the state variable so next call is a different row.
            ++rowIndex;
        }, // End callback definition
        simpleSheet: true
   });
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50648230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档