Javascript 在google应用程序脚本上同步函数调用

Javascript 在google应用程序脚本上同步函数调用,javascript,asynchronous,google-apps-script,google-sheets-api,Javascript,Asynchronous,Google Apps Script,Google Sheets Api,我正在处理一个大约有700行的工作表,每行的第一列都有一个短语。 我正试图编写一个脚本,通过给定一个与我所拥有的类似的工作表,将在右边添加一列,其中包含此行中的字数,在添加每行的计数后,它将根据添加的列按降序对该工作表进行排序 发生的事情是,显然存在异步性,它首先按第二列排序,然后再添加计数。。。我环顾四周,找不到解决这个问题的好办法。。。有什么想法吗 守则: function sortByNumOfKeywords(lang, col, sheet) { var file = findF

我正在处理一个大约有700行的工作表,每行的第一列都有一个短语。 我正试图编写一个脚本,通过给定一个与我所拥有的类似的工作表,将在右边添加一列,其中包含此行中的字数,在添加每行的计数后,它将根据添加的列按降序对该工作表进行排序

发生的事情是,显然存在异步性,它首先按第二列排序,然后再添加计数。。。我环顾四周,找不到解决这个问题的好办法。。。有什么想法吗

守则:

function sortByNumOfKeywords(lang, col, sheet) {

  var file = findFileByName("Translate Keywords List");

  var spreadsheet = SpreadsheetApp.openById(file.getId());

  //Getting the sheet name from the list above
  var sheet = findSheetByName(spreadsheet,'Spanish');

  //Getting Table bounds (Two-Dimensional array);
  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();

  //add one column (Column H) to hold the number of words of each keyword in the first column
  addWordsCounterCol(sheet, col, lastRow, lastColumn);

  //sorting by the added column(H)
  sheet.sort(8, false);

}

//sets a new column at the end of the table with the number of keywords of each cell in the given Row
function addWordsCounterCol(sheet, col, lastRow, lastCol){ 
  sheet.activate();
  var colChar = String.fromCharCode(65 + col-1); //Converts the Col number to it cohherent Big letter
  var formulas = []; //Formulas will be set as a two-dimensional array
  for(var i=2; i<lastRow+1; i++){
    var cell = sheet.getRange("" +colChar + i);
    var numOfKeys = "=COUNTA(SPLIT(trim("+ colChar + i + "), \" \"))";
    formulas[i-2] = []; //initializing row with a new array
    formulas[i-2].push(""+numOfKeys); // each row has only one column, with this specific String
  }
  var cells = sheet.getRange("H2:H"+(lastRow));

  cells.setFormulas(formulas);
}
函数排序关键字(lang、col、sheet){
var file=findFileByName(“翻译关键字列表”);
var spreadsheet=SpreadsheetApp.openById(file.getId());
//从上面的列表中获取图纸名称
var表=findSheetByName(电子表格,“西班牙语”);
//获取表边界(二维数组);
var lastRow=sheet.getLastRow();
var lastColumn=sheet.getLastColumn();
//添加一列(H列)以保存第一列中每个关键字的字数
AddWordsIntercol(表、列、最后一行、最后一列);
//按添加列排序(H)
表.排序(8,假);
}
//在表的末尾设置一个新列,其中包含给定行中每个单元格的关键字数
函数addwordscontercol(sheet,col,lastRow,lastCol){
sheet.activate();
var colChar=String.fromCharCode(65+col-1);//将列号转换为同调大字母
var formulas=[];//公式将设置为二维数组
对于(var i=2;i请尝试使用。应用所有挂起的电子表格更改

在您的代码中,它对我很有用:

...
SpreadsheetApp.flush();
//sorting by the added column(H)
sheet.sort(8, false);
...
@请结账