简体   繁体   中英

Data Scraping With ImportHTML in Apps Script & Google Sheets

Goal : I am trying to pull data from a website and use it to create a big table. I can tell that I'm very close to getting this to work, but I've reached a roadblock.

Background : I have a google sheet with three pages. (1) Titled "tickers" is a list of every ticker in the S&P 500, in rows A1-A500. (2) Titled actionField is just a blank page used during the script. (3) Titled resultField will hold the results. The website I am pulling from is ( http://www.reuters.com/finance/stocks/companyOfficers?symbol=V ) Though, I want the script to work (with minor modification) for any data accessible through importHtml.

Script : The script I currently have is as follows:

function populateData() {
var googleSheet = SpreadsheetApp.getActive();

// Reading Section

var sheet = googleSheet.getSheetByName('tickers');

var tickerArray = sheet.getDataRange().getValues();
var arrayLength = tickerArray.length;
var blankSyntaxA = 'ImportHtml("http://www.reuters.com/finance/stocks/companyOfficers?symbol=';
var blankSyntaxB = '", "table", 1)';

// Writing Section

for (var i = 0; i < arrayLength; i++)
  {
     var sheet = googleSheet.getSheetByName('actionField'); 
     var liveSyntax = blankSyntaxA+tickerArray[i][0]+blankSyntaxB;
     sheet.getRange('A1').setFormula(liveSyntax);
     Utilities.sleep(5000);
     var importedData = sheet.getDataRange().getValues();
     var sheet = googleSheet.getSheetByName('resultField'); 
     sheet.appendRow(importedData)
  }  
}

This successfully grabs the ticker from the tickers page. Calls importHtml. Copies the data. And appends SOMETHING to the right page. It loops through and does this for each item in the ticker list.

However, the data being appended is as follows:

[Ljava.lang.Object;@42782e7c
[Ljava.lang.Object;@2de9f184
[Ljava.lang.Object;@4b86a4d0

That displays across many columns, for as many rows as there are iterations in the loop.

How do I successfully append the data?

(And any advice on improving this script?)

The appendRow method is not suitable here. As it only appends one row, its argument is expected to be a 1D array of values.

What you get from getValues is normally a 2D array of values, like [[a,b], [c,d]] . Even if it's just one row, getValues will return [[a,b]] . The only exception is a single-cell range, for which you get just the value in that cell. It's never a 1D array.

If just one row is needed, use, eg, appendRow(importedData[0]) .

Otherwise, insert the required number of rows and assign the 2D array of values to them.

 var sheet = googleSheet.getSheetByName('resultField'); 
 var lastRow = sheet.getLastRow();
 sheet.insertRowsAfter(lastRow, importedData.length);
 sheet.getRange(lastRow + 1, 1, importedData.length, importedData[0].length)
      .setValues(importedData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM