我在 Google Apps Script 中有以下代码,它使用基本身份验证通过 HTTP 从网页中检索 CSV 数据并将其放入电子表格中:
CSVImport.gs
function parseCSVtoSheet(sheetName, url)
{
// Credentials
var username = "myusername";
var password = "mypassword";
var header = "Basic " + Utilities.base64Encode(username + ":" + password);
// Setting the authorization header for basic HTTP authentication
var options = {
"headers": {
"Authorization": header
}
};
// Getting the ID of the sheet with the name passed as parameter
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// …Run Code Online (Sandbox Code Playgroud) 我正在处理一个项目,要求我在API方法调用中使用JavaScript.我是一名Java程序员,之前从未进行过Web开发,所以我遇到了一些麻烦.
这个API方法是异步的,它在while循环中.如果它返回一个空数组,则while循环结束.否则,它循环.码:
var done = true;
do
{
async_api_call(
"method.name",
{
// Do stuff.
},
function(result)
{
if(result.error())
{
console.error(result.error());
}
else
{
// Sets the boolean to true if the returned array is empty, or false otherwise.
done = (result.data().length === 0) ? true : false;
}
}
);
} while (!done);
Run Code Online (Sandbox Code Playgroud)
这不起作用.循环在"完成"的值更新之前结束.我已经完成了一些关于这个主题的阅读,看来我需要使用promises或回调,因为API调用是异步的,但是我无法理解如何将它们应用到我上面的代码中.
帮助将不胜感激!