我希望获取Google Word文档的内容并将其放在textBox中。以下代码吐出错误:
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
var docContents = docName[0].getContentAsString(); //get contents of document
tapp.getElementById("songboxID").setValue(songfile2); //set the value of the textBox to the contents of the document
return tapp;
}
这会返回以下错误:
Unsupported conversion requested.
我在某处读过我们无法为Google文档执行此操作,但我们可以查看我们上传的其他非Google文档。是吗?
这是我不能再发布5个小时的答案,因为我是新手并没有声誉:
在Serge的帮助下,这对我有用:
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
var docName = DocsList.find(e.parameter.doclist); //get document name based on what user clicked on in listBox
var docId = docName[0].getId(); //get document ID
var content = DocumentApp.openById(docId).getText(); //get contents of document
tapp.getElementById("songboxID").setValue(content); //set web app's textBox (called songboxID) to match the contents of the document the user clicked on
return tapp;
}
答案 0 :(得分:0)
您必须使用DocumentApp.getText();
来获取文档的文本内容。
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
Logger.log(e.parameter.doclist)
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
var docId = docName[0].getId();
Logger.log(docName[0].getName()+' = '+docId)
var textContent = DocumentApp.openById(docId).getText();
Logger.log(textContent)
tapp.getElementById("songboxID").setValue(textContent); //set the value of the textBox to the contents of the document
return tapp;
}
编辑:正如您在实验中注意到的那样DocsList.find(e.parameter.doclist)
会返回您没有预料到的结果......这只是因为find
运算符不仅会搜索文档的名称还有文档内容。
要解决这个问题,您应该添加一个smal例程,根据文档名称检查查找查询的结果,如下所示:
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
for(var d in docName){{
if(docName[d].getName()==e.parameter.doclist){
var docId = docName[d].getId();
}
}
这将确保您正在寻找的文件实际上是正确的文件。
PS:很抱歉没有立即提及......它刚刚脱离了我的脑海; - )