현재 불행히도 너무 느리게 실행되는 Google 스크립트를 작성하고 있습니다. (6 분 초과). 스크립트는 문서를 열고 두 개의 문자열 (Google 시트에 설정 됨)을 대체하여 PDF로 저장합니다. 멋진 일은 없어.Google 스크립트가 너무 느리게 실행 중입니다.
이 스크립트를 실행하는 데 약 200 개의 문서가 있지만 6 분 내에 6 개로 만 관리됩니다. Google 스크립트가이 속도가 느리거나, 실수로 가장 비효율적 인 Google 스크립트를 만든 적이 있습니까?
function createAllPDF() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = SpreadsheetApp.getActiveSheet().getActiveRange()
var numRows = SpreadsheetApp.getActiveSpreadsheet().getLastRow() - 1;
for(var i = 9; i <= numRows; i++) {
var thisRange = sheet.getRange("A" + n + ":C" + n);
var fond = thisRange.getCell(1, 1).getValue();
var adresse = thisRange.getCell(1, 3).getValue();
thisRange.setBackground('#cfe2f3');
genDoc(fond, adresse);
}
}
//// CREATE PDF ////////////////// FUNCTION FOR GENERATING THE PDF /////////////////////////////////
function genDoc(fond, adresse) {
// Finds the template and duplicate it into a new file.
var template = ("xxxxxxxxxxxx");
var docId = DriveApp.getFileById(template).makeCopy().getId();
// Opens the newly created Document for editing
var doc = DocumentApp.openById(docId);
var body = doc.getActiveSection();
// Renames the newly generated document
var newName = doc.setName(fond);
// Replaces each with the parsed variables.
body.replaceText("%FOND%", fond);
body.replaceText("%ADRESSE%", adresse);
doc.saveAndClose();
//Adds the PDF ID to the invoice_input sheet
var conv = DriveApp.getFileById(docId);
var pdf = conv.getAs("application/pdf");
var fileId = DriveApp.createFile(pdf).getId();
// Gets the PDF file by ID
var thisPDF = DriveApp.getFileById(fileId);
// The ID of the folder I'd like to put the PDF into.
var folderId = "xxxxxxxxxxx";
// Gets the folder by ID
var targetFolder = DriveApp.getFolderById(folderId);
// Adds the PDF to the Folder
targetFolder.addFile(thisPDF);
// Removes the PDF from the root.
var root = DriveApp.getRootFolder().removeFile(thisPDF);
// Deletes the duplicated document
DriveApp.getFileById(docId).setTrashed(true)
return fileId;
}
최적화 방법에 대한 모든 정보가 크게 도움이 될 것입니다. 나는 아주 일반적으로 구글 스크립트와 프로그래밍에 익숙하지 않기 때문에 큰 단어는 없다. 이 보드를 잘못 사용하면 사과드립니다. 알려 주시면 수정하겠습니다.
[코드 검토] (http://codereview.stackexchange.com/help/on-topic) – pnuts