gpt4 book ai didi

javascript - 在谷歌应用程序脚本上同步函数调用

转载 作者:行者123 更新时间:2023-12-03 01:41:54 26 4
gpt4 key购买 nike

我正在处理一张大约有 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);
}

最佳答案

尝试使用flush() 。应用所有待处理的电子表格更改。

它在你的代码中对我来说工作得很好:

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

关于javascript - 在谷歌应用程序脚本上同步函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50802426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com