首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用google应用程序脚本从web地址的pdf文件中提取文本,并将其插入Google工作表中。

使用google应用程序脚本从web地址的pdf文件中提取文本,并将其插入Google工作表中。
EN

Stack Overflow用户
提问于 2021-11-14 14:56:24
回答 1查看 287关注 0票数 1

在下面的示例中,我将文件夹和ss保留为空。

想法是检索文本“新兴市场”之后的数字(在代码中指定的url文件中找到),然后将其插入到指定的google表的单元格b2中。

没有得到任何错误,但代码不起作用。会很感激你的帮助。新手来了。

谢谢!

代码语言:javascript
复制
const FOLDER_ID = ""; //Folder ID of all PDFs
const SS = "";//The spreadsheet ID
const SHEET = "MSCI";//The sheet tab name


function OpenFile() {
 var url = "https://www.yardeni.com/pub/mscipe.pdf";
  
  var blob = UrlFetchApp.fetch(url).getBlob();
  var resource = {
    title: blob.getName(),
    mimeType: blob.getContentType()
  };

  // Enable the Advanced Drive API Service
  var file = Drive.Files.insert(resource, blob, {ocr: true, ocrLanguage: "en"});

  // Extract Text from PDF file
  var doc = DocumentApp.openById(file.id);
  var text = doc.getBody().getText();
  
return text;

const identifier = {
    start: `Emerging Markets (`,
    start_include: false,
    end: `)`,
    end_include: false
  };
  let results = getDocItems(docID, identifier);
  return results;
  }

function importToSpreadsheet(results){
  const sheet = SpreadsheetApp.openById(SS).getSheetByName(SHEET);
 
  var cell = sheet.getRange("B2");
  cell.setValue(results);
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-14 16:15:08

我看到两个函数:OpenFile()importToSpreadsheet(results),但没有看到调用函数的行。

只是猜一下。也许您需要在代码的末尾添加以下一行:

代码语言:javascript
复制
importToSpreadsheet(OpenFile());

更新

OpenFile()函数将为您获取所有文本。如果您只需要使用“新兴市场()”和“”之间的文本部分,就可以这样删除它:

代码语言:javascript
复制
var text = OpenFile(); // all text
var part = text.split('Emerging Markets (')[1].split(')')[0]; // a part between 'Emerging Markets (' and ')'
importToSpreadsheet(part); // put the part in the cell

const identifier = {......return results;的行是多余的。可能它们是从另一个样本中提取的,不属于这个代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69964203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档