How can I collect all of the data on a Google spreadsheet and then clear it after the script has been run?

为君一笑 提交于 2021-02-05 09:36:19

问题


I am creating a script that will grab all unread e-mails, move the bodies of these e-mails into a Google spreadsheet, push all of this data through to Slack via webhook, then clear the sheet.

The first part of the script that grabs the unread e-mails and puts it in the appropriate cells is working fine. The part where it is pushing the data through is not working. I have it set to a loop to make sure that it grabs all of the data, but it is not working. I have tried to make it not show up as an array, but that didn't seem to work either.

var SEARCH_QUERY = "label:inbox is:unread to:me";

function getEmails_(q) {
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) {
            emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')
            ]);
        }
    }
    return emails;
}

function appendData_(sheet, array2d) {
    sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}

function saveEmails() {
    var array2d = getEmails_(SEARCH_QUERY);
    if (array2d) {
        appendData_(SpreadsheetApp.getActiveSheet(), array2d);
    }
}

function postToSlack() {
    var url = "SLACK WEBHOOK URL HERE";
    var sheet = SpreadsheetApp.getActiveSheet();
     // This represents ALL the rows
    var range = sheet.getDataRange();
    var values = range.getValues();
    for (var i = 0; i < values.length; i++) {
    var row = [];
    for (var j = 0; j < values[i].length; j++) {
     if (values[i][j]) {
       row.push(values[i][j]);
     }
   }
   Logger.log(row);

    var str = values;
    var payload = {
        "text": str  
    }



    var post = {
        "method": "post",
        "payload": JSON.stringify(payload)

    };



    return UrlFetchApp.fetch(url, post);
}

}

I expect that the data in the cells will push right out into Slack, but nothing is happening. When I run the Logs for the rows that it finds, it is also pulling up no data.


回答1:


I figured it out! It wanted a toString() command. The following works perfectly EXCEPT that it does not clear the sheet at the end. It will only clear if a separate function is running.

var SEARCH_QUERY = "label:inbox is:unread to:me";
var url = "SLACK WEBHOOK URL";

function getEmails_(q) 
{
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) 
        {
          emails.push([msgs[j].getPlainBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]);
            msgs[j].markRead();   
        }
    }
      return emails;
}

function appendData_(sheet, array2d) {
    sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}

function saveEmails() {
    var array2d = getEmails_(SEARCH_QUERY);
    if (array2d) {
        appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = sheet.getDataRange();
    var values = range.getValues();
    var row = [];
    for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[i].length; j++) {
     if (values[i][j]) {
       row.push(values[i][j]);
     }
    }

   }
   Logger.log(row);

    var str = values.toString();
    var payload = {
        "text": str  
    }



//    var post = {
//        "method": "post",
//        "payload": JSON.stringify(payload)
//      
//    };



//    return UrlFetchApp.fetch(url, post); 




function clear() {
  var app = SpreadsheetApp;
  var sheet = app.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange("A1:A40").clearContent();}




  }



回答2:


Edit: In your added answer, you can either delete clear() and add in sheet.getRange("A1:A40").clearContent(); below Logger.log(row);. Another way is that you can also take out the clear() out of the saveEmails() and call like this,

function saveEmails() {
var array2d = getEmails_(SEARCH_QUERY);
if (array2d) {
    appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var row = [];
for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[i].length; j++) {
        if (values[i][j]) {
            row.push(values[i][j]);
        }
    }

}
Logger.log(row);

var str = values.toString();
var payload = {
    "text": str
}

clear();
//    var post = {
//        "method": "post",
//        "payload": JSON.stringify(payload)
//      
//    };


//    return UrlFetchApp.fetch(url, post); 
}

function clear() {
    var app = SpreadsheetApp;
    var sheet = app.getActiveSpreadsheet().getActiveSheet();
    sheet.getRange("A1:A40").clearContent();
}


来源:https://stackoverflow.com/questions/56172155/how-can-i-collect-all-of-the-data-on-a-google-spreadsheet-and-then-clear-it-afte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!