Google Apps 脚本是否有承诺? [英] Are there promises on Google Apps Script?

查看:32
本文介绍了Google Apps 脚本是否有承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入数据的表单,我使用 onFormSubmit 触发一个脚本,该脚本根据插入的数据创建 CSV,在创建 CSV 文件后,我删除了数据.问题是我在创建 CSV 文件之前删除了数据.通常我只会做一个承诺电话,但我认为 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:

有关其功能的更多见解:当我收到一个新的表单条目时,Avaliacao"表会更新并触发 testTrigger().

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

然后,它将在 LastUser 城市中写入电子邮件和用户城市,以便它可以查找一些我将用于构建 CSV 文件的数据.但是 saveAsCsv 函数在工作表完成其 VLOOKUP 调用之前被调用.所以我的 CSV 文件是空的.

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.

同步的另一个问题是,如果我启用 sLastUser.clear(); 行,它也会在创建 CSV 之前删除.

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(",") + "
";
        }
        else {
          csv += data[row];
        }
      }
      csvFile = csv;
    }
    return csvFile;
  }
  catch(err) {
    Logger.log(err);
    Browser.msgBox(err);
  }
}

推荐答案

确实,Google Apps Script 使用的 JS 引擎不支持 promises(Logger.log(Promise); 显示未定义).

Indeed, the JS engine used by Google Apps Script does not support promises (Logger.log(Promise); shows it's undefined).

要确保电子表格更改在继续之前生效,您可以使用 SpreadsheetApp.flush().

To make sure that spreadsheet changes take effect before proceeding further, you can use SpreadsheetApp.flush().

另一个相关功能是事件对象Form Submit 上的触发器:它将提交的数据传递给脚本,而不必将其从电子表格中取出.

Another relevant feature is event object passed by the trigger on Form Submit: it delivers the submitted data to the script without it having to fish it out of the spreadsheet.

这篇关于Google Apps 脚本是否有承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆