Google sheets Arduino谷歌表单集成

Google sheets Arduino谷歌表单集成,google-sheets,arduino,project,Google Sheets,Arduino,Project,我正在和Arduino一起做一个项目,我试图将变量的数据发布到GoogleSheet集成中,但代码不起作用 我试图纠正它,但它没有张贴无论如何…这是代码 错误是 它不是一个函数 我从Arduino IoT云谷歌表单集成中获取了代码 function myFunction() { // get sheet named RawData var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&q

我正在和Arduino一起做一个项目,我试图将变量的数据发布到GoogleSheet集成中,但代码不起作用

我试图纠正它,但它没有张贴无论如何…这是代码

错误是

它不是一个函数

我从Arduino IoT云谷歌表单集成中获取了代码

function myFunction() {
        // get sheet named RawData
        var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dati");

        var MAX_ROWS = 1440;     // max number of data rows to display
        // 3600s / cloud_int(30s) * num_ore(12h)
        var HEADER_ROW = 1;     // row index of header
        var TIMESTAMP_COL = 1;  // column index of the timestamp column

        function doPost(e) {  
          var cloudData = JSON.parse(e.postData.contents); // this is a json object containing all info coming from IoT Cloud
          //var webhook_id = cloudData.webhook_id; // really not using these three
          //var device_id = cloudData.device_id;
          //var thing_id = cloudData.thing_id;
          var values = cloudData.values; // this is an array of json objects
          
          // store names and values from the values array
          // just for simplicity
          var incLength = values.length;
          var incNames = [];
          var incValues = [];
          for (var i = 0; i < incLength; i++) {
            incNames[i] = values[i].name;
            incValues[i] = values[i].value;
          }
          
          // read timestamp of incoming message
          var timestamp = values[0].updated_at;          // format: yyyy-MM-ddTHH:mm:ss.mmmZ
          var date = new Date(Date.parse(timestamp)); 
          
          /*
          This if statement is due to the fact that duplicate messages arrive from the cloud!
          If that occurs, the timestamp is not read correctly and date variable gets compromised.
          Hence, execute the rest of the script if the year of the date is well defined and it is greater
          then 2018 (or any other year before)
          */
          if (date.getYear() > 2018) {
          
            // discard all messages that arrive 'late'
            if (sheet.getRange(HEADER_ROW+1, 1).getValue() != '') { // for the first time app is run
              var now = new Date(); // now
              var COMM_TIME = 5; // rough overestimate of communication time between cloud and app
              if (now.getTime() - date.getTime() > COMM_TIME * 1000) {
                return;
              }
            }
            
            // this section write property names 
            sheet.getRange(HEADER_ROW, 1).setValue('timestamp');
            for (var i = 0; i < incLength; i++) {
              var lastCol = sheet.getLastColumn(); // at the very beginning this should return 1 // second cycle -> it is 2
              if (lastCol == 1) {
                sheet.getRange(HEADER_ROW, lastCol + 1).setValue(incNames[i]);
              } else {
                // check if the name is already in header
                var found = 0;
                for (var col = 2; col <= lastCol; col++) {
                  if (sheet.getRange(HEADER_ROW, col).getValue() == incNames[i]) {
                    found = 1;
                    break;
                  }
                }
                if (found == 0) {
                  sheet.getRange(HEADER_ROW, lastCol+1).setValue(incNames[i]);
                }
              }
            }
            
            // redefine last column and last row since new names could have been added
            var lastCol = sheet.getLastColumn();
            var lastRow = sheet.getLastRow();
            
            // delete last row to maintain constant the total number of rows
            if (lastRow > MAX_ROWS + HEADER_ROW - 1) { 
              sheet.deleteRow(lastRow);
            }
            
            // insert new row after deleting the last one
            sheet.insertRowAfter(HEADER_ROW);
            
            // reset style of the new row, otherwise it will inherit the style of the header row
            var range = sheet.getRange('A2:Z2');
            //range.setBackground('#ffffff');
            range.setFontColor('#000000');
            range.setFontSize(10);
            range.setFontWeight('normal');
            
            // write the timestamp
            sheet.getRange(HEADER_ROW+1, TIMESTAMP_COL).setValue(date).setNumberFormat("yyyy-MM-dd HH:mm:ss");
            
            // write values in the respective columns
            for (var col = 1+TIMESTAMP_COL; col <= lastCol; col++) {
              // first copy previous values
              // this is to avoid empty cells if not all properties are updated at the same time
              sheet.getRange(HEADER_ROW+1, col).setValue(sheet.getRange(HEADER_ROW+2, col).getValue());
              for (var i = 0; i < incLength; i++) {
                var currentName = sheet.getRange(HEADER_ROW, col).getValue();
                if (currentName == incNames[i]) {
                  // turn boolean values into 0/1, otherwise google sheets interprets them as labels in the graph
                  if (incValues[i] == true) {
                    incValues[i] = 1;
                  } else if (incValues[i] == false) {
                    incValues[i] = 0;
                  }
                  sheet.getRange(HEADER_ROW+1, col).setValue(incValues[i]);
                } 
              }
            }  
          
          } // end if (date.getYear() > 2018)
        }  
}
函数myFunction(){
//获取名为RawData的工作表
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Dati”);
var MAX_ROWS=1440;//要显示的最大数据行数
//3600秒/云量(30秒)*数量(12小时)
var HEADER_ROW=1;//表头的行索引
var TIMESTAMP_COL=1;//TIMESTAMP列的列索引
函数doPost(e){
var cloudData=JSON.parse(e.postData.contents);//这是一个JSON对象,包含来自IoT云的所有信息
//var webhook_id=cloudData.webhook_id;//确实没有使用这三个
//var device\u id=cloudData.device\u id;
//var thing\u id=cloudData.thing\u id;
var values=cloudData.values;//这是一个json对象数组
//存储值数组中的名称和值
//只是为了简单起见
var incLength=值.length;
var incNames=[];
var incValues=[];
对于(变量i=0;i2018){
//丢弃所有“迟到”的邮件
if(sheet.getRange(HEADER_ROW+1,1).getValue()!=“”){//第一次运行应用程序
var now=新日期();//现在
var COMM_TIME=5;//粗略高估了云和应用程序之间的通信时间
if(now.getTime()-date.getTime()>COMM_TIME*1000){
返回;
}
}
//本节写属性名
sheet.getRange(标题行,1).setValue('timestamp');
对于(变量i=0;i它是2
if(lastCol==1){
sheet.getRange(HEADER_行,lastCol+1).setValue(incNames[i]);
}否则{
//检查名称是否已在标题中
var=0;
对于(var col=2;col MAX_ROWS+HEADER_ROW-1){
表.删除行(最后一行);
}
//删除最后一行后插入新行
sheet.insertRowAfter(标题行);
//重置新行的样式,否则它将继承标题行的样式
var范围=sheet.getRange('A2:Z2');
//范围.倒退('#ffffff');
range.setFontColor('#000000');
范围。设置字体大小(10);
范围。设置字体重量(“正常”);
//写时间戳
sheet.getRange(标题行+1,时间戳列).setValue(日期).setNumberFormat(“yyyy-MM-dd HH:MM:ss”);
//在相应的列中写入值
用于(变量列=1+时间戳\u列;列2018)
}  
}

您正试图将数据直接推送到云上的Google工作表,但出于安全原因,它的URL使用https。https的加密相当复杂,需要SSL加密。Arduino硬件通常不够快,无法执行SSL。您需要更强大的硬件,如Raspberry Pi,以直接写入Google云


但是,如果你坚持在IOT项目中使用Arduino(任何变体),你可以使用PushingBox,一种可以根据API调用发送通知的云,为你做艰苦的工作。在这里,你将数据发送到PushingBox,它使用http URL,然后将数据反过来发送到Google工作表。

在你的脚本中,
函数doPost(e){,,}
被放在
myFunction
的函数中。在这种情况下,如何执行
myFunction
doPost
的函数?并且,
ss.sheet.getSheetByName的错误行在哪里?它不是脚本中的一个函数
?Idk如何编程javascript?我只是从这里复制了arduino代码,而不是我知道第一行和第二行显示了错误谢谢你的回答。我不得不为我糟糕的英语技能道歉。不幸的是,从你的回答中,我仍然无法理解你的问题。但我想试着去理解它。当我能正确理解它时,我想想出解决办法。我深深地道歉,我无法很快解决您的问题。您从何处运行此代码?这似乎不太可能,因为他们1)使用的Arduino直接连接到网络2)通过连接的计算机(而不是微控制器本身)连接…他们在评论中发布的链接使1看起来更像是一款功能更强大、与Arduino兼容、带有32位ARM芯片的设备!