我正在尝试让应用程序脚本在表中设置一个值,然后在成功时转到BOMOnSuccess
它将重新加载已修改表的值的函数。
我遇到的问题是代码在实际设置值之前似乎继续运行,因此检索到的表不是最新的,并且我的 HTML 页面中显示的内容不准确。
当我“刷新” HTML 页面时,它又是正确的。我需要做的是在提示符中输入新值,以便设置单元格值并且该新值反映在列表框中。
Utilities.sleep
我在 .gs ( ) 和 javascript中都尝试过各种不同的暂停。这些似乎都没有什么不同。
有时它实际上会暂停服务器设置值,即使我在设置它的行之后睡眠也是如此。其他时候,如果我让它正确暂停,它仍然有旧数据。
SpreadsheetApp.flush
之后我也尝试过setValue
,但没有任何区别。
.gs:
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
function showForm() {
var html = HtmlService.createTemplateFromFile('Form')
.evaluate();
html
.setTitle('AP Pricing Form')
.setWidth(600)
.setHeight(500);
var dialog = ui.showModalDialog(html, "AP Pricing App")
}
function getBom(){
var sheet = ss.getSheets()[4]
var vals = sheet.getDataRange().getValues();
return vals
}
function updateMaterialQuantity(qty,prod,mat){
var sheet = ss.getSheets()[4]
var values = sheet.getDataRange().getValues();
for(var i = 1; i<values.length; i++){
if(values[i][0] == prod){
if(values[i][1] == mat){
sheet.getRange(i+1,3).setValue(qty);
SpreadsheetApp.flush();
return;
}
}
}
}
function include(filename){
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
};
js:
//get BOM from BOM table
function refreshBOM(prodOption){
for (a in document.getElementById("productMaterials").options) { document.getElementById("productMaterials").options.remove(a); }
prod = prodOption
function filterBOM(array){
var newBom = [];
for(var i = 1; i<array.length; i++){
if(array[i][0] == prod){
newBom.push(array[i])
}
}
return newBom;
}
function BOMOnSuccess(array){
bom = array
bom = filterBOM(bom);
for(var i = 0; i < bom.length; i ++){
var opt = document.createElement("option");
opt.text = bom[i][2] + " - " + String(bom[i][1]);
opt.text = opt.text.toUpperCase();
opt.value = bom[i][1];
document.getElementById("productMaterials").options.add(opt);
}
}
google.script.run.withSuccessHandler(BOMOnSuccess).getBom();
}
//edit materials
function editProductQty(material){
if(material != ""){
var qty = prompt("Enter new quantity");
var prod = document.getElementById("productsSelect").value
google.script.run.withSuccessHandler(refreshBOM(prod)).updateMaterialQuantity(qty,prod,material);
}
else{
alert("Please choose a product and material");
}
}
预期的是editProductQty
在单击按钮时运行。这将导致用户输入新值。然后updateMaterialQuantity
会运行。
这将转到 .gs 并找到范围并设置新值。然后refreshBOM
运行它将获得新更新的范围,新值将显示在 HTML 页面上。
实际发生的是当getBOM
被调用时,它以某种方式获取了旧的未更新版本的表。我认为问题在于,由于某种原因,js代码完成后会发生值的实际设置,但我不确定。