我设法为特定文档创建模板生成器(用例结构)(学生的项目规范)。它工作正常,但问题是当使用ui.prompt()与用户交互时它很慢。
建议不要使用浏览器提示,因为它有时会被阻止。那么我该如何改进我的代码?
我也尝试记录每个调用提示的时间戳,但它似乎不适用于ui调用。
只需复制粘贴我的烂摊子试试吧......(文字是法语)
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
function onOpen() {
var menu = DocumentApp.getUi().createMenu('Modèle');
menu.addItem("Use case", 'addUseCaseTemplate').addToUi();
menu.addItem("Use case (Wizard)", 'addUseCaseWizard_ui').addToUi();
menu.addItem("TEST", 'test').addToUi();
}
function addUseCaseTemplate() {
logFunction(arguments.callee.toString());
var data = {
title : '',
};
var index = getCursorIndex();
addUseCaseTemplate_full(index, data);
}
function addUseCaseWizard_ui() {
logFunction(arguments.callee.toString());
addUseCaseWizard();
}
// Utilisation de l'assistant de cas d'utilisation
function addUseCaseWizard() {
var wizardTitle = 'Assistant';
var ui = DocumentApp.getUi();
var data = {
title : '',
description : '',
actorsMain : '',
actorsSecondary : '',
};
// Titre
Logger.log("Prompt --> Title");
var resp_title = ui.prompt(wizardTitle, "What is the use case title?", ui.ButtonSet.OK_CANCEL);
if (resp_title.getSelectedButton() == ui.Button.OK) {
data.title = resp_title.getResponseText();
var resp_desc = ui.prompt(wizardTitle, "Write a brief", ui.ButtonSet.OK_CANCEL);
var resp_actPrinc = ui.prompt(wizardTitle, "Who are the main actors? (Eg : Manager, Salesman)", ui.ButtonSet.OK_CANCEL)
var resp_actSec = ui.prompt(wizardTitle, "Who are the secondary actors? (Ex : Manager, Salesman)", ui.ButtonSet.OK_CANCEL)
if (resp_desc.getSelectedButton() == ui.Button.OK)
data.description = resp_desc.getResponseText();
if (resp_actPrinc.getSelectedButton() == ui.Button.OK)
data.actorsMain = resp_actPrinc.getResponseText();
if (resp_actSec.getSelectedButton() == ui.Button.OK)
data.actorsSecondary = resp_actSec.getResponseText();
// Ajout de la structure ici!
var index = getCursorIndex();
addUseCaseTemplate_full(index, data);
}
}
function test() {
DocumentApp.getUi().alert("Cursor index : " + getCursorIndex());
logFunction(arguments.callee.toString());
}
// Retourne l'index du curseur
function getCursorIndex() {
logFunction(arguments.callee.toString());
var cursor = doc.getCursor();
var index = -1;
if (cursor)
{
var uc = cursor.insertText("").getParent().asParagraph();
index = body.getChildIndex(uc);
}
return index;
}
// Permet de générer un modèle complet de cas d'utilisation.
// Paramètres :
// @index : Index de départ où insérer la structure
// @data : JSON avec tous les éléments pour rédiger l'information
// Retour :
// Index de la fin de la structure
function addUseCaseTemplate_full(index, data) {
logFunction(arguments.callee.toString());
index = addUseCaseTitle_full(index, data.title) + 1;
index = addSummary_full(index, data) + 1;
index = addEmptyParagraph(index) + 1;
index = addUseCaseDetailed(index) + 1;
index = addActors(index, data) + 1;
index = addEmptyParagraph(index) + 1;
index = addPreCondition(index) + 1;
index = addEmptyParagraph(index) + 1;
index = addScenario(index) + 1;
index = addEmptyParagraph(index) + 1;
index = addAlternativeChain(index) + 1;
index = addEmptyParagraph(index) + 1;
index = addErrorChain(index) + 1;
index = addEmptyParagraph(index) + 1;
return index;
}
function addUseCaseTitle(index) {
logFunction(arguments.callee.toString());
return addUseCaseTitle_full (index, '');
}
function addUseCaseTitle_full(index, title) {
logFunction(arguments.callee.toString());
var text = "Use case : " + title;
var p = body.insertParagraph(index, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING3);
return index;
}
function addSummary(index) {
logFunction(arguments.callee.toString());
return addSummary_full (index, data);
}
function addSummary_full(index, data) {
logFunction(arguments.callee.toString());
var text = "Summary";
var p = body.insertParagraph(index++, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING4);
var cells = [
['Title : '],
['Brief : '],
['Actors : '],
["Création date : \t\t\t Update : "],
["Version : \t\t\t\t Responsible : "]
];
var table = body.insertTable(index, cells);
// Données du tableau
if (data) {
// TITRE
var title = data.title ? data.title : ' ';
table.getRow(0).getCell(0).getChild(0).asParagraph()
.appendText(title).setBold(false);
// Description
var description = data.description ? data.description : ' ';
table.getRow(1).getCell(0).getChild(0).asParagraph()
.appendText(description).setBold(false);
// Acteurs
var actors = data.actorsMain ? data.actorsMain : '';
actors += data.actorsSecondary ? (actors != '' ? ', ' : '') + data.actorsSecondary : ''
actors = actors != '' ? actors : ' ';
table.getRow(2).getCell(0).getChild(0).asParagraph()
.appendText(actors).setBold(false);
}
return index;
}
// Titre Cas d'utilisation détaillé
function addUseCaseDetailed(index) {
logFunction(arguments.callee.toString());
var text = "Detailed use case";
var p = body.insertParagraph(index, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING4);
return index;
}
// Ajoute une structure acteur
// Retourne l'index du dernier élément
function addActors (index, data) {
logFunction(arguments.callee.toString());
var text = "Actors";
var p = body.insertParagraph(index++, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
var ls = body.insertListItem(index++, 'Main : ').setGlyphType(DocumentApp.GlyphType.BULLET);
body.insertListItem(index, 'Secondary : ').setListId(ls);
if (data)
{
if (data.actorsMain)
{
ls.getChild(0).asText().appendText(data.actorsMain).setBold(false);
}
if (data.actorsSecondary)
{
ls.getNextSibling().asText().appendText(data.actorsSecondary).setBold(false);
}
}
return index;
}
// Ajout de la section precondition
function addPreCondition (index) {
logFunction(arguments.callee.toString());
var text = "Preconditions";
var p = body.insertParagraph(index++, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
var ls = body.insertListItem(index, '').setGlyphType(DocumentApp.GlyphType.BULLET);
return index;
}
// Ajout de la section precondition
function addPostCondition (index) {
logFunction(arguments.callee.toString());
var text = "Postconditions";
var p = body.insertParagraph(index++, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
var ls = body.insertListItem(index, '').setGlyphType(DocumentApp.GlyphType.BULLET);
return index;
}
// Ajout de la section scenario nominal
function addScenario (index) {
logFunction(arguments.callee.toString());
var text = "Main scenario ";
var p = body.insertParagraph(index++, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
var ls = body.insertListItem(index, '');
return index;
}
// Ajout de la section enchaînement alternative
function addAlternativeChain (index) {
logFunction(arguments.callee.toString());
var text = "Alternative scenario";
var p = body.insertParagraph(index, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
return index;
}
// Ajout de la section enchaînement d'erreurs
function addErrorChain(index) {
logFunction(arguments.callee.toString());
var text = "Error scenario";
var p = body.insertParagraph(index, text);
p.setHeading(DocumentApp.ParagraphHeading.HEADING5);
return index;
}
// Ajout d'un paragraphe vide
function addEmptyParagraph(index) {
logFunction(arguments.callee.toString());
var text = "";
var p = body.insertParagraph(index, text);
p.setHeading(DocumentApp.ParagraphHeading.NORMAL);
return index;
}
function logFunction(callee) {
var log = {
fnt : filterCalleeName(callee),
};
Logger.log(log);
return log;
}
function filterCalleeName(callee) {
var temp = callee.substr('function '.length);
temp = temp.substr(0, temp.indexOf('('));
return temp;
}
感谢您的帮助!
答案 0 :(得分:0)
您需要停止使用提示。 使用包含所有输入字段的uiApp和一个输入它们的按钮构建一个ui。 您可以使用自定义对话框,甚至可以更好地创建永久侧边栏:https://developers.google.com/apps-script/guides/dialogs#custom_sidebars