首页 > 解决方案 > 数据未写入 Google 电子表格

问题描述

我的脚本将来自标记文件夹的邮件中的“未读”传入消息复制到谷歌表,然后将其标记为已读。但有时会出现故障:脚本被执行,将字母标记为“已读”,但不写入表。也就是说,事实上,在记录器中有一条记录了这些字母的所有内容,但它们并没有写入表中。理论上,您需要检查数据是否已写入表中,然后才将字母标记为“已读”。也许代码中有一个错误会定期让自己感觉到?各位大侠帮帮我,我只是在学习。

        function GmailmarkReadEnd() {
  
  //this is just the stuff that recognizes what spreadsheet you're in
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName('Лист2'); //gets the right sheet
  
  /* searches your GMail for emails matching things "label:unread" + " label:support"
  */
  var query = "label:unread" + " label:support"; 
  
  var threads = GmailApp.search(query);
  
  var supportStats = [];
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    
    for (var m = 0; m < messages.length; m++) {
      var from = messages[m].getFrom(); //from field
      var to = messages[m].getTo(); //to field
      var time = messages[m].getDate(); //date field
      var subject = messages[m].getSubject(); //subject field
      var body = messages[m].getPlainBody(); //body field
      var mId = messages[m].getId(); //id field to create the link later
      
      if (query === "label:unread" + " label:support") {
        supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId]);
        Logger.log(supportStats) // The log about which he spoke.
      }
      
    }
  }
  if(!threads.length) return; //  if there are no unread ones, do nothing.
  sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.
                 length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
  
  GmailApp.markThreadsRead(threads); // marks as read
  
  // ***Sorting Recorded Data by Date***
  sheet.getRange('D:D').activate();
  sheet.sort(4, false);
}

标签: google-apps-scriptgoogle-sheets

解决方案


function GmailmarkReadEnd() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Лист2'); //gets the right sheet
  const query = "label:unread" + " label:support"; 
  var threads = GmailApp.search(query);
  var supportStats = [];
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var m = 0; m < messages.length; m++) {
      var from = messages[m].getFrom(); //from field
      var to = messages[m].getTo(); //to field
      var time = messages[m].getDate(); //date field
      var subject = messages[m].getSubject(); //subject field
      var body = messages[m].getPlainBody(); //body field
      var mId = messages[m].getId(); //id field to create the link later
      supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId]);
      Logger.log(supportStats) // The log about which he spoke.
    }
  }
  if(!threads.length) return; //  if there are no unread ones, do nothing.
  sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
  GmailApp.markThreadsRead(threads); // marks as read
  sheet.sort(4,false);
}

推荐阅读