我有一个GAS,我要在自己的Web应用程序中提交一个表单。它执行以下操作:
##form_field_name##
)的模板文档,该占位符与表单上表单字段的名称相对应body.replaceText('##' + key + '##', val);
将值填充到文档中body.replaceText('##.*##', '');
来删除所有留空的字段的占位符字符串(因为它们当前未与表单一起提交)我最后得到的文档看起来用户根本没有提交任何数据。我的猜测是这是因为#4在#3完成之前正在运行。有什么办法将#4指定为回调?类似于:
body.replaceText('##' + key + '##', val, () => {
body.replaceText('##.*##', '');
});
这里是执行replaceText()
处理的功能的代码:
/**
* Iterate through obj of keys/values and replace ##key##
* with value in doc
**/
function doFindAndReplace(obj, doc) {
try {
//body = doc.getBody();
var id = doc.getId();
var _doc = DocumentApp.openById(id);
var body = _doc.getBody();
//var doc = _doc;
//Logger.log(body);
for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
var val = obj[prop];
var key = prop.indexOf('intake') === 0 ? prop.toUpperCase() : '';
//Logger.log(key + ' = ' + val);
//Logger.log(typeof val);
body.replaceText('##' + key + '##', val);
_doc.saveAndClose();
var _doc2 = DocumentApp.openById(id);
body = _doc2.getBody();
body.replaceText('##.*##', '');
}
}
} catch(e) {
Logger.log('doFindAndReplace() | ' + e);
errors.push({
fxn: 'doFindAndReplace',
msg: e,
custom: 'id=' + id
});
return false;
}
return doc;
}
obj
参数是包含提交的表单数据的对象,doc
是对其中进行查找和替换的新创建文档的引用。
[如果没有类似的办法,我正在考虑添加一个将运行#4(onOpen
)的body.replaceText('##.*##', '');
触发器。这将要求用户在线时首次打开该文档,并且用例的很大一部分正在离线使用新创建的文档。
有人可以建议其他解决方法吗?
** 编辑 **我将代码(上面已反映)更新为saveAndClose()
,然后在运行第二个replaceText()
之前重新打开。我收到以下错误:
文档已关闭,其内容无法更新
我知道,您会认为它会在saveAndClose()
之后[[之后。但是,这就是错误所指向的行,即使我删除了它之后的其余行,也会发生相同的错误。
saveAndClose()
行,则会再次遇到我原来的问题。我认为这与Weird Behaviour with DriveApp.getFileById()没有相同的问题
**
编辑2 **这是传递给函数的对象的示例:
{
intake_activity_level: "Normal",
intake_assessment_comments: "",
intake_client_addr: "1600 Fake St XXXX, XX 12345",
intake_client_email: "[email protected]",
intake_client_name: "Dan Tester",
intake_client_phone: "(720) 336-0485",
intake_consent: "yes",
intake_consent_owner: "Jim David",
intake_food_intake: "Decreased",
intake_gen_food: "",
intake_gen_health: "sit dolam amet",
intake_gen_meds: "re et sapien et, consectetur rhoncus lacus. Aliqua Vivamus ipsum diam, venenatis a turpis eget, volu",
intake_gen_reasons: "Lorem ipsum",
intake_gen_vaccine: "Yes",
intake_misc_comments: "",
intake_misc_descriptor: "wood",
intake_panting: "Increased",
intake_pet_breed: "Pug",
intake_pet_dob: "8/11/2015",
intake_pet_name: "FLuffy",
intake_pet_sex: "Spayed Female",
intake_pet_species: "Dog",
intake_pet_weight: "140lbs",
intake_sleep: "Increased",
intake_source: "Sally Balls",
intake_stiffness: "Normal",
intake_symptoms: "coughing, belching or gas",
intake_vet: "Urban Vet",
intake_voice: "Increased",
intake_water_intake: "Decreased"
}
并且可以使用文档的缩小版本here。
##.*##
将''
替换为body.replaceText('##.*##', '');
。body.replaceText('##.*##', '');
之后运行body.replaceText('##' + key + '##', val);
。body.replaceText('##.*##', '');
放在for循环中。这样,仅使用intake_activity_level: "Normal",
,不使用其他对象。在这种情况下,在运行第一个body.replaceText('##' + key + '##', val);
之后,所有的##key##
值都将被body.replaceText('##.*##', '');
删除。我认为这可能是您遇到问题的原因。修改脚本后,请进行如下修改。
从:for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
var val = obj[prop];
var key = prop.indexOf('intake') === 0 ? prop.toUpperCase() : '';
//Logger.log(key + ' = ' + val);
//Logger.log(typeof val);
body.replaceText('##' + key + '##', val);
_doc.saveAndClose();
var _doc2 = DocumentApp.openById(id);
body = _doc2.getBody();
body.replaceText('##.*##', '');
}
}
至:
for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
var val = obj[prop];
var key = prop.indexOf('intake') === 0 ? prop.toUpperCase() : '';
body.replaceText('##' + key + '##', val);
}
}
body.replaceText('##.*##', '');
在此修改中,首先,body.replaceText('##' + key + '##', val);
在for循环中运行。然后,body.replaceText('##.*##', '');
在for循环的外部运行。