Код: Выделить всё
index.htmlКод: Выделить всё
Uploadling...
function readFileAsBuffer(file){
return new Promise(function(resolve,reject){
let fr = new FileReader();
fr.onload = function(){
resolve(fr.result);
};
fr.onerror = function(){
reject(fr);
};
fr.readAsArrayBuffer(file);
});
}
async function importCsv(e) {
document.getElementById("progress").style.display = "block";
const files = e.file.files;
let fileReaders = [];
let fileValues = [];
for(let i = 0;i < files.length;i++){
fileReaders.push(readFileAsBuffer(files[i]));
}
await Promise.all(fileReaders).then((values) => {
fileValues.push(values);
});
for(let i=0; i < files.length; i++)
{
let file = files[i];
await google.script.run.withSuccessHandler(google.script.host.close).importCsv([[...new Int8Array(fileValues[0][i])], file.type, file.name]);
}
}
< /code>
script.gsfunction onOpen() {
SpreadsheetApp.getUi().createMenu("Import").addItem("Import CSVs", "importCsv").addToUi();
}
function importCsv(e){
if (!e) {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("index"), "Import CSVs");
return;
}
const lock = LockService.getScriptLock();
lock.waitLock(30000);
const csv = Utilities.parseCsv(Utilities.newBlob(...e).getDataAsString());
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 3, 1, csv.length, csv[0].length).setValues(csv);
lock.releaseLock();
}
< /code>
I initially had it set up not using promises, but I was finding that all of the files I tried to import would get placed into the spreadsheet out of order. I thought that by using promises to wait until each file was read and processed before passing over to the spreadsheet would help with this, but I'm still running into the same issue. Other than using the LockService object like I'm doing, does anyone have any ideas on how to make the script wait until each section is finished getting added to the spreadsheet?
Thanks!
Подробнее здесь: https://stackoverflow.com/questions/795 ... pps-script
Мобильная версия