我有4行在谷歌电子表格,并希望迭代通过他们每1个小时一次。这段代码目前可以工作,但同时发布了所有这四个代码。我如何才能每小时1个小时,并使其连续?
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)
发布于 2018-06-07 07:25:04
根据TableTop文档(字面意思是在readme of the repo中),使用simpleSheet: true
可以向回调函数提供工作簿第一个工作表中的行数组。
你的代码:
callback: (data, tabletop) => {
data.forEach(d =>
从而指示要处理该工作表上的每一行。
要在每次执行callback
时只调用一行的函数,您需要定义其他一些限定了作用域的变量--让我们称其为rowIndex
。您应该在加载bot服务器时初始化此变量,然后在callback
中对其进行递增和边界检查。
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
});
};
https://stackoverflow.com/questions/50648230
复制相似问题