How to read Google Form Data Using App Script

≡放荡痞女 提交于 2019-12-13 08:48:08

问题


I'm new to App Script. I'm trying to read the submitted data using App Script. I tried to use the answer provided here, but I'm not able to read it.

I tried many other method, they are also not working.

function myFunction() {
  var form = FormApp.openById('1_HABIBYRZ-zsEMuzMCnsrAOYFlQSKwaIhmeT1y2SY6Y');
  var formResponses = form.getResponses();

  MailApp.sendEmail ("test@appirio.com", "Form Submited: Foo feedback " + Date.now(), +formResponses);
}

I also want to know, how to debug any issue in App Script.

Thanks in Advance!


回答1:


The other answer above may work, but it's a bit complicated. To let you keep the form responses as a variable inside your program, which would allow you to email it, you can do this:

 function formResponsesToArray() {
  var app = FormApp.openById(...),
  responses = app.getResponses(), stringResponses = []

  responses.forEach(function(r) {
    var response = []
    r.getItemResponses().forEach(function(i) {
      response.push(i.getResponse())
    })

    stringResponses.push(response)
  })

  Logger.log(stringResponses)
}

Which will log an array of responses like this:




回答2:


The way I'd recommend doing it is feed your form responses into a google sheet, and then export is as a csv via attachment in an email. like so below. It is important to make sure the correct API's are active, such as Drive API, Gmail API and that your oauthScopes are added to the manifest if need be.


editing my answer to recommend stewarts answer. much cleaner, simpler and way more efficient.

function ExportToCSVCrntMonth(){
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); //change this to sheet name
 // Change the below line to get the folder where you want the CSV files stored.
 var folder = DriveApp.createFolder(ss.getName().toLowerCase().replace(/ /g,'_') + '_csv_' + new Date().getTime());
 fileName = ss.getName() + ".csv";
 var csvFile = convertRangeToCsvFileCrnt_(fileName, ss);
 folder.createFile(fileName, csvFile);

 var csvmail = folder.getFilesByName('your sheet name + .csv');
  MailApp.sendEmail({
  to: "your email ere",
  subject: "CSV attached",
  body: "body text",

  attachments: [csvmail.next()]
  });
 }

function convertRangeToCsvFileCrnt_(csvFileName, ss) {
var activeRange = ss.getDataRange();
 try {
   var data = activeRange.getValues();
   var csvFile = undefined;

   if (data.length > 1) {
   var csv = "";
   for (var row = 0; row < data.length; row++) {
    for (var col = 0; col < data[row].length; col++) {
      if (data[row][col].toString().indexOf(",") != -1) {
        data[row][col] = "\"" + data[row][col] + "\"";
       }
      }

    if (row < data.length-1) {
      csv += data[row].join(",") + "\r\n";
    }
    else {
      csv += data[row];
     }
    }
   csvFile = csv;
  }
   return csvFile;
  }
   catch(err) {
   Logger.log(err);
   Browser.msgBox(err);
    }
  }


来源:https://stackoverflow.com/questions/52110698/how-to-read-google-form-data-using-app-script

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