0

Google 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.

Googleドキュメントではこれを実行できないとどこかで読みましたが、アップロードする他のGoogle以外のドキュメントでは実行できます。そうですか?


私は新しくて評判がないので、あと5時間は投稿できないという答えは次のとおりです。

セルジュの助けを借りて、これが私のために働いたものです:

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;
}
4

1 に答える 1

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:すぐにそれについて言及しなかったことをお詫びします...それはちょうど私の心から滑り落ちました;-)

于 2013-03-20T18:23:42.190 に答える