同步 google 应用脚本的函数调用
Synchronizing function calls on google app script
我正在使用大约有 700 行的 sheet,每行的第一列都有一个短语。
我正在尝试编写一个脚本,通过给定一个类似于我所拥有的 sheet - 将在右侧添加一个额外的列,其中包含该行中的单词数,并在每行上添加计数后,它将根据添加的列按降序对 sheet 进行排序。
发生了什么,显然存在异步性,它首先按第二列排序,然后才添加计数……我环顾四周,找不到解决这个问题的合适方法……有什么想法吗?
代码:
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);
}
尝试使用 flush()
。应用所有未决的电子表格更改。
在你的代码中对我来说效果很好:
...
SpreadsheetApp.flush();
//sorting by the added column(H)
sheet.sort(8, false);
...
我正在使用大约有 700 行的 sheet,每行的第一列都有一个短语。 我正在尝试编写一个脚本,通过给定一个类似于我所拥有的 sheet - 将在右侧添加一个额外的列,其中包含该行中的单词数,并在每行上添加计数后,它将根据添加的列按降序对 sheet 进行排序。
发生了什么,显然存在异步性,它首先按第二列排序,然后才添加计数……我环顾四周,找不到解决这个问题的合适方法……有什么想法吗?
代码:
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);
}
尝试使用 flush()
。应用所有未决的电子表格更改。
在你的代码中对我来说效果很好:
...
SpreadsheetApp.flush();
//sorting by the added column(H)
sheet.sort(8, false);
...