Google apps script 将文档内容放入文本框中

Google apps script 将文档内容放入文本框中,google-apps-script,Google Apps Script,我正在寻找一个谷歌word文档的内容,并把它放在一个文本框中。以下代码显示错误: 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 =

我正在寻找一个谷歌word文档的内容,并把它放在一个文本框中。以下代码显示错误:

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.
我在某个地方读到,我们不能为谷歌文档这样做,但我们可以为我们上传的其他非谷歌文档这样做。是这样吗


以下是我的答案,因为我是新来的,没有名声,所以我不能再发布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;
}

您必须使用
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例程,该例程根据文档名称检查find查询的结果,如下所示:

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:很抱歉没有立即提到这一点。。。我刚刚忘记了;-)

我确信就是这样,但每次我尝试时都会得到“遇到错误:文档丢失(可能被删除了?)”,即使我将ID放入其中(而不是docID)。你知道为什么会这样吗?好的。。。这是我做的工作。如果我创建了一个全新的文档,它就会工作(从这里得到这个想法)。现在的问题是,它没有抓住整个文档。唉,现在突然它正在处理以前没有的文档。。。我想知道谷歌是否在做什么。尽管我在获取整个文档时仍然存在问题。我在代码中添加了一些日志,以便您可以看到发生了什么以及失败的位置/原因。很抱歉添加所有这些注释(但我觉得我应该这样做,而不是编辑以前的注释),现在它不起作用了。我没有做任何改变,肯定有不稳定的事情发生。我正在尝试将其部署为web应用程序,当我单击“为您测试web应用程序最新代码”链接时,遇到了这些问题。也许这就是问题所在?仅供参考,我从电脑上传了一个通用的ini文件,我可以很好地看到它的内容。我也尝试了.getAs(),但如果是这样做的话,我不知道应该将其设置为什么(我尝试了application/vnd.google-apps.document,但没有效果)。为了了解情况,网页有三列。左栏是列表框(非下拉),中栏是文本区域,右栏当前未使用。左列填充了某个文件夹中的用户文档(创建文件夹是当前的预要求,我相信我可以稍后编写代码)。当用户单击其中任何一个时,文档的内容将添加到中心列中进行编辑。我想你现在可以说它只是一个用于文档的GUI,但稍后我会在中间的专栏中做其他的事情。