私は現在、残念ながら遅すぎる実行するGoogleスクリプトを書いています。 (6分以上の制限)。スクリプトはドキュメントを開き、2つの文字列(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;
}
どのように最適化するかの指針は非常に高く評価されます。私は非常に新しいスクリプトとプログラミングのプログラミングに一般的なので、大きな言葉は、heheです。 このボードを誤って使用している場合は、お詫び申し上げます。私に知らせて訂正します。
[コードレビュー](http://codereview.stackexchange.com/help/on-topic) – pnuts