Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there promises on Google Apps Script?

I have a form that my users enter data and I use onFormSubmit to trigger a script that creates a CSV based on the data inserted and after I create the CSV file I delete the data. Problem is that I am deleting the data before creating CSV file. Usually I would just make a promise call but I think it is not possible with Google Apps Script. Is there any alternative to it?

So my complete code is here:

More insight about what it does: When I receive a new form entry, the "Avaliacao" sheet gets updated and will trigger testTrigger().

Then, it will write the email and the usercity in the LastUser city so it can lookup for some data that I will use to build my CSV file. But the saveAsCsv function is called before the sheet completed its VLOOKUP calls. So my CSV file is empty.

Another issue that I have with synchronization is that if I enable the sLastUser.clear(); line it will also delete before creating the CSV.

function testTrigger () {
  //open the sheets
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var sAvaliacao = SS.getSheetByName("Avaliação");  
  var sPreCSV = SS.getSheetByName("PreCSV");  
  var sInput = SS.getSheetByName("Input");    
  var sLastUser = SS.getSheetByName("LastUser"); 
  var dAvaliacao = sAvaliacao.getDataRange().getValues();
  var dInput = sInput.getDataRange().getValues();

  var avaliacaoLastRow = sAvaliacao.getLastRow()-1;

  var userEmail = dAvaliacao[avaliacaoLastRow][2];
  var userCity = dAvaliacao[avaliacaoLastRow][5];
  var userId = dInput[3][52];

  sLastUser.appendRow([userEmail, userCity]);

  saveAsCSV(userId);
//  sLastUser.clear();   <== this is the line where I can`t enable
}

function saveAsCSV(csvName) {
  // Name
  var fileName = String(csvName) + ".csv"
  // Calls convertcsv
  var csvFile = convertOutputToCsv_(fileName);
  // create the file on my drive
  DriveApp.createFile(fileName, csvFile);
}

function convertOutputToCsv_(csvFileName) {
  // open sheets
  var sPreCSV = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PreCSV");
  var cont = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input").getDataRange().getValues()[3][50] + 1;

  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PreCSV").getRange(1, 1, cont, 3);
  try {
    var data = ws.getValues();
    var csvFile = undefined;

    // Loop through the data in the range and build a string with the CSV data
    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] + "\"";
          }
        }

        // Join each row's columns
        // Add a carriage return to end of each row, except for the last one
        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);
  }
}
like image 645
Hugo Nakamura Avatar asked Apr 12 '16 00:04

Hugo Nakamura


People also ask

Is it worth learning Google Apps Script?

It's worth learning so you can automate tedious parts of your G Suite workflows, or build useful tools for yourself or colleagues at work. It's challenging enough that you'll never run out of ideas or avenues to explore, but friendly enough you can make inroads pretty quickly.

Is it safe to use Google Apps Script?

Security Flaw in Google Apps Script can Let Hackers Deliver Malware via SaaS Platform. Security researchers reported a security flaw in Google Apps Script that can enable hackers and cybercriminals to deliver Google Drive-hosted malware when abused and exploited.

How long can a Google Apps Script run?

A single execution of Apps script can last no longer than 6 minutes and you're hitting this limit.


1 Answers

Now with engine v8 Promise is defined object

like image 189
Avazbek Ismanov Avatar answered Oct 13 '22 10:10

Avazbek Ismanov