Javascript 处理上传的文本文件后在Google drive中创建新文档

Javascript 处理上传的文本文件后在Google drive中创建新文档,javascript,google-apps-script,runtime-error,google-apps,Javascript,Google Apps Script,Runtime Error,Google Apps,我成功地上传了一个文本文件到谷歌硬盘,我已经写了一个方法,成功地将文本翻译成拉丁语。现在,我正试图在GoogleDrive中创建一个新文档来输出翻译后的文本。然而,我总是收到信息:“发生错误”,当我检查我的驱动器时,我只有原始上传的文本 这是我的密码: function doGet(e) { var app = UiApp.createApplication().setTitle("Upload"); var formContent = app.createVerticalPanel(

我成功地上传了一个文本文件到谷歌硬盘,我已经写了一个方法,成功地将文本翻译成拉丁语。现在,我正试图在GoogleDrive中创建一个新文档来输出翻译后的文本。然而,我总是收到信息:“发生错误”,当我检查我的驱动器时,我只有原始上传的文本

这是我的密码:

function doGet(e) {

 var app = UiApp.createApplication().setTitle("Upload");
   var formContent = app.createVerticalPanel();
   formContent.add(app.createFileUpload().setName('thefile'));
   formContent.add(app.createSubmitButton('submit'));
   var form = app.createFormPanel();
   form.add(formContent);
   app.add(form);
   return app;
 }

function doPost(e) {
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   var doc = DocsList.createFile(fileBlob);

   var app = UiApp.getActiveApplication();
   //Display a confirmation message
   var label = app.createLabel('file uploaded successfully');
   app.add(label);
   return app;

  var text = doc.getDataAsString();
  Logger.log('I uploaded and my text is: ' + text);

  MakeTranslationDoc(text);
 }

function MakeTranslationDoc(passedText) 
{ 

  // Create a new Report 
  var newdoc = DocumentApp.create('Pig Latin Translation');

  newdoc.appendParagraph(ParseText(passedText));

  // Save and close the document
  newdoc.saveAndClose();
}

function ParseText(myText) 
{  
  ...convert text to piglatin...
  return results;
}

如何从上传的文本成功创建新文档?

这些天要做的第一件事是移动到比文档列表更新的DriveApp

失败的函数是doc.getDataAsString(),它已被doc.getAs(mimeType)替换

这两个更改是什么,并将文档ID传递给下一个函数(失去耦合?),您的代码将开始工作

function doGet(e) {

 var app = UiApp.createApplication().setTitle("Upload");
   var formContent = app.createVerticalPanel();
   formContent.add(app.createFileUpload().setName('thefile'));
   formContent.add(app.createSubmitButton('submit'));
   var form = app.createFormPanel();
   form.add(formContent);
   app.add(form);
   return app;
 }

function doPost(e) {
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   var doc = DriveApp.createFile(fileBlob);

   var app = UiApp.getActiveApplication();
   //Display a confirmation message
   var label = app.createLabel('file uploaded successfully');
   app.add(label);

  docID = doc.getId()
  MakeTranslationDoc(docID);

  return app;
 }

function MakeTranslationDoc(docID) 
{

  var uploadedDoc =  DriveApp.getFileById(docID);
  var text = uploadedDoc.getAs(uploadedDoc.getMimeType());

  // Create a new Report 
  var newdoc = DocumentApp.create('Pig Latin Translation');

  newdoc.appendParagraph(ParseText(text));

  // Save and close the document
  newdoc.saveAndClose();
}

function ParseText(myText) 
{  
//  ...convert text to piglatin...
  return myText;
}
重要的是不要假设您知道MimeType并从文档本身获取它


如果遇到任何问题,请告诉我。

这些天要做的第一件事是转到比文档列表更新的DriveApp

失败的函数是doc.getDataAsString(),它已被doc.getAs(mimeType)替换

这两个更改是什么,并将文档ID传递给下一个函数(失去耦合?),您的代码将开始工作

function doGet(e) {

 var app = UiApp.createApplication().setTitle("Upload");
   var formContent = app.createVerticalPanel();
   formContent.add(app.createFileUpload().setName('thefile'));
   formContent.add(app.createSubmitButton('submit'));
   var form = app.createFormPanel();
   form.add(formContent);
   app.add(form);
   return app;
 }

function doPost(e) {
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   var doc = DriveApp.createFile(fileBlob);

   var app = UiApp.getActiveApplication();
   //Display a confirmation message
   var label = app.createLabel('file uploaded successfully');
   app.add(label);

  docID = doc.getId()
  MakeTranslationDoc(docID);

  return app;
 }

function MakeTranslationDoc(docID) 
{

  var uploadedDoc =  DriveApp.getFileById(docID);
  var text = uploadedDoc.getAs(uploadedDoc.getMimeType());

  // Create a new Report 
  var newdoc = DocumentApp.create('Pig Latin Translation');

  newdoc.appendParagraph(ParseText(text));

  // Save and close the document
  newdoc.saveAndClose();
}

function ParseText(myText) 
{  
//  ...convert text to piglatin...
  return myText;
}
重要的是不要假设您知道MimeType并从文档本身获取它


如果您遇到任何问题,请告诉我。

有时间再次调查:(这有点麻烦,但应该可以解决。)

  • 您将需要使用API控制台设置一个项目,以便获取您的使用者密钥和使用者密钥……同样重要的是,为回调正确地设置重定向URI和来源,并且分别对我有效:)
这是API控制台,您应该创建一个项目,该项目要求您使用google凭据登录。如果这是您第一次登录API,您将获得一个大按钮来创建您的第一个项目。然后,您应该获取web应用程序客户端的凭据。当您单击API访问链接时,会找到消费者密钥和消费者密钥,它们被称为“客户端ID”和“客户端机密”

  • 请确保选择驱动器API服务
创建项目后,控制台将显示,单击服务(右侧链接),向下滚动并单击驱动器API切换以启用它

  • 使用上载文件的ID,您可以使用urlFetch下载其内容
您刚刚上传的文件作为ID,您可以使用docID=doc.getId()获取它

  • 您需要授权OAuth流
基本上,在您第一次运行脚本时,它将通过授权流程,这将使您的应用程序能够访问您的驱动器资源,因为您可能会看到应用程序选择的范围是只读的“www.googleapis.com/auth/drive.readonly”您可以了解有关驱动器API的更多信息

  • 我在电子表格中这样做是为了查看Logger语句,当然您可以将其移植到非电子表格绑定的脚本
在电子表格中开发提供了更好的调试功能,之后我们可以将代码移植到独立脚本。不同之处在于,要显示UI,您需要调用活动电子表格并在其上调用.show(应用程序)。。。有关此信息,请单击此处:

代码:


让我知道它是如何工作的。

有时间再研究一下:(这有点像黑客,但应该有用

  • 您将需要使用API控制台设置一个项目,以便获取您的使用者密钥和使用者密钥……同样重要的是,为回调正确地设置重定向URI和来源,并且分别对我有效:)
这是API控制台,您应该创建一个项目,该项目要求您使用google凭据登录。如果这是您第一次登录API,您将获得一个大按钮来创建您的第一个项目。然后,您应该获取web应用程序客户端的凭据。当您单击API访问链接时,会找到消费者密钥和消费者密钥,它们被称为“客户端ID”和“客户端机密”

  • 请确保选择驱动器API服务
创建项目后,控制台将显示,单击服务(右侧链接),向下滚动并单击驱动器API切换以启用它

  • 使用上载文件的ID,您可以使用urlFetch下载其内容
您刚刚上传的文件作为ID,您可以使用docID=doc.getId()获取它

  • 您需要授权OAuth流
基本上,在您第一次运行脚本时,它将通过授权流程,这将使您的应用程序能够访问您的驱动器资源,因为您可能会看到应用程序选择的范围是只读的“www.googleapis.com/auth/drive.readonly”您可以了解有关驱动器API的更多信息

  • 我在电子表格中这样做是为了查看Logger语句,当然您可以将其移植到非电子表格绑定的脚本
在电子表格中开发提供了更好的调试功能,之后我们可以将代码移植到独立脚本。不同之处在于,要显示UI,您需要调用活动电子表格并在其上调用.show(应用程序)。。。有关此信息,请单击此处:

代码:


让我知道它是如何工作的。

恐怕您的代码对我不起作用。这是您自己要测试的urll。我复制了你所有的代码,除了解析文本。你有没有gmail可以和我分享脚本?这样你就可以看到和测试真实的东西了?我对fast说,事实上,内容不是以文本的形式返回的,只是Blob这个词。。。这里列出了一些讨论和一些解决方案,它们面向使用urlfetch下载刚刚上传的文档内容。对不起,abo