【问题标题】:extract data from Google Docs into Google Sheets using script?使用脚本将 Google Docs 中的数据提取到 Google 表格中?
【发布时间】:2022-10-25 12:30:05
【问题描述】:

寻找 Doc -> Sheets 脚本。

我有大约 300 个 Google Doc 文件,其中包含一些类似的部分,我想从中提取一些数据到 Google Sheet 中。 (例如,他们在第一页上有地址和人名,我只想提取地址和姓名,而不是文档的其余部分)

我希望有一个脚本或其他自动方式从所有 300 个 Google 文档中依次读取信息,以便我可以提取相关部分并将其保存到 Google 表格中。 (单独导出每个文件将花费太长时间)

有什么建议或想法吗?

【问题讨论】:

  • 我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解您来自I have about 300 Google Doc files with some similar parts that I'd like to extract some data from into a Google Sheet. I'd like to have a script or other automated way (saving each file individually will take far too long) of reading in the information so I can have the relevant part extracted. 的问题。我可以问你问题的细节吗?
  • 我在我的问题中添加了更多信息以希望澄清。
  • 谢谢你的回复。我不得不再次为我糟糕的英语水平道歉。不幸的是,我仍然无法理解你的问题。但我想试着理解它。当我能正确理解它时,我想考虑解决方案。如果您能原谅我糟糕的英语水平,我将不胜感激。
  • 我正在寻找 Doc -> Sheets 脚本。在文档中:第 1、2、3 行到表格中的 ABC 列。我不知道还能说什么。
  • 谢谢你的回复。不幸的是,来自I'm looking for a Doc -> Sheets script. I'm not sure what else to say.,我仍然无法理解您的问题。这是因为我的英语水平不好。我再次为此深表歉意。但我愿意支持你。所以我想试着理解它。当我能正确理解它时,我想考虑解决方案。如果您能原谅我糟糕的英语水平,我将不胜感激。

标签: google-sheets import doc


【解决方案1】:

所以我做了一些挖掘并找到了一种方法来做到这一点:可能有一种更简单的方法来做到这一点,但这对我有用。

function readGoogleDoc_writetoGoogelSheet() {
  
  // sheet with all IDs of documents
  var IDdocSheet = SpreadsheetApp.openById('-----doc ID goes here----');
  var IDdoc = IDdocSheet.getSheetByName('Sheet1'); //Sheet tab with source data

//target sheet
  var TargetdocSheet = SpreadsheetApp.openById('----doc ID goes here ----');
  var Targetdoc = TargetdocSheet.getSheetByName('Sheet1'); //Sheet tab with target data



    startRow = 0; // First row of data to process
    numRows = 251; // Number of rows to process

    // Fetch the range of cells into an array containing all IDs
    const dataRange = IDdoc.getRange(1, 1, numRows, 1);   //getRange(row, column, numRows, numColumns)

    // Fetch values for each row in the Range.
    const data = dataRange.getValues(); 
    var text=[];

   var start=1;
   for (i=1;i<numRows; i++){
      var docID= data[i];
      Logger.log(i+"  "+docID);
      var doc = DocumentApp.openById(docID);

//get body of text from the Google DOC
      const body = doc.getBody();
//convert all content to text
      var text = body.editAsText().getText();

//set up convert all lines of text (as separated by a new line character (/n)
      var line=[];
      var l=0; 
      line[l]="";
      var string="";
      var max_l=0;

      for (let j = 0; j < 5000; j++) {

        if (text[j]!="
"){
          string=string+text[j];
        }else{ 
          line[l]=string;
          s=string;
          string="";
          l++;
        }
        //I cust off when I find some text in my document... in my case when the line begins with either the work "Welcome" or "Next", then I stop processing and move to the next file
        if((l>2) && ( (s.substring(0, 7)=="Welcome") || (s.substring(0, 4)=="Next"))){max_l=l-3;break;}
      }
      
//write relevant daate to targetspreadsheet
      for (let l = 0; l < max_l; l++) {
          if(   line[l].search(/---text to find----/)>0 ){
            var insertpart=line[l].substring(line[l].search(/---text to find----/)+8, line[l].length);
              var cell = Targetdoc.getRange("A"+start);
              cell.setValue(insertpart);

              start+=1;
          }
      }
   }
}

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 2021-10-19
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2022-10-18
    • 2013-12-06
    相关资源
    最近更新 更多