Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9760

Creating a looping macro

$
0
0

I created a script to add a price tag in a sheet where I keep track of cards I like to collect, retrieving data from a website using the IMPORTHTML function:

function searchprice() { 
  var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('TCG PLAYER PRICES'), true); 
    spreadsheet.getRange('A2').activate(); 
  spreadsheet.getCurrentCell().setFormula('=IMPORTHTML(\'CARDS\'!L2;"table";2)');
    spreadsheet.getRange('B3').activate(); 
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('CARDS'), true);
    spreadsheet.getRange('K2').activate(); 
    spreadsheet.getRange('\'TCG PLAYER PRICES\'!B3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false); 

The point is the sheet "CARDS" has 5991 lines of card information in it, and I cannot imagine copying and pasting the code that number a time... So I tried to use the looping, but with no success.

function searchprice() {
  var spreadsheet = SpreadsheetApp.getActive();

  for (r=2; r<5992; r++) {
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('TCG PLAYER PRICES'), true);
    spreadsheet.getRange('A2').activate();
 spreadsheet.getCurrentCell().setFormula('=IMPORTHTML(\'CARDS\'!R[2]C[12];"table";2)');
    spreadsheet.getRange('B3').activate();
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('CARDS'), true);
    spreadsheet.getRange(2,11).activate();
    spreadsheet.getRange('\'TCG PLAYER PRICES\'!B3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  }
}

For better understanding:

Sheet TCG PLAYER PRICES is where the price data is
A2 is where the function IMPORTHTML is. It retrieves a 2x2 information table but I need only 1 information (that is in B3).

imgbb

Sheet CARDS is where my inventory is
L(12th column) is where the "hyperlink" card name column to retrieve the website data from is. K(11th column) is where I wanna copy the price I have in "TCG PLAYER PRICES - B3". And so on (L2, K2; L3, K3; L4; K4...L3000; K3000)

imgbb


Viewing all articles
Browse latest Browse all 9760

Trending Articles