我正在尝试在我的 Google 脚本中创建一个触发器,该触发器将调用另一个脚本中的函数,重置一个值。我正在使用记录事件/小时的粒子光子。剧本很重要。Google 脚本使用时间驱动触发器每小时从光子收集数据。我的问题是,一旦收集了所述小时的数据,计时器就不会重置。有没有办法让 Google 脚本在每次获取数据后最终将计数器重置为 0?
这是谷歌脚本:
function collectData() {
var sheet = SpreadsheetApp.getActiveSheet();
var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/xxxxxxxxxx/result?access_token=xxxxxxxxxx");
try {
var response = JSON.parse(response.getContentText()); // parse the JSON the Core API created
var result = unescape(response.result); // you'll need to unescape before your parse as JSON
try {
var p = JSON.parse(result); // parse the JSON you created
var d = new Date(); // time stamps are always good when taking readings
sheet.appendRow([d, p.count]); // append the date, count to sheet
} catch(e)
{
Logger.log("Unable to do second parse");
}
} catch(e)
{
Logger.log("Unable to returned JSON");
}
}
这是光子脚本:
int Gate = D4;
int Led = D7;
int gateState = 0;
int count = 0;
char resultstr[64];
void setup() {
pinMode(Gate, INPUT);
pinMode(Led, OUTPUT);
Particle.variable("result", resultstr, STRING);
}
void loop() {
static byte prevState = 1;
gateState = digitalRead(Gate);
digitalWrite(Led, LOW);
if(gateState != prevState)
{
if(gateState == HIGH)
{
count ++;
digitalWrite(Led, HIGH);
}
prevState = gateState;
}
sprintf(resultstr, "{\"count\" :%d}", count);
delay(50);
}