TulsaNewbie
TulsaNewbie

Reputation: 401

Google Apps Script and parser: easy way to build a 2d array from two 1d arrays?

I'm using Parser to build some arrays from data on a website (and the cUseful cache service to keep from having to read the page every time I want to run the program):

function readCrew() {
  var categoryCrewURL = "https://stt.wiki/wiki/Category:Crew"
  var categoryCrewText = "Category-Crew-List";
  var wholePagefromText = 'next page';
  var wholePagetoText = 'previous page';

  var content = getCacheData(categoryCrewURL, categoryCrewText);
  var scraped = Parser
                .data(content)
                .from(wholePagefromText)
                .to(wholePagetoText)
                .build();

  var fromText = 'href="';
  var toText = '"';
  var crewURLs = Parser
                 .data(scraped)
                 .from(fromText)
                 .to(toText)
                 .iterate();
  Logger.log(crewURLs);
  var fromText = 'title="';
  var toText = '">';
  var crewNames = Parser
                  .data(scraped)
                  .from(fromText)
                  .to(toText)
                  .iterate();
  Logger.log(crewNames);
  ssActive = SpreadsheetApp.getActiveSpreadsheet();
  rgMyRange = ssActive.getRange("A:A");
  rgMyRange.setValues(crewNames);
}

function getCacheData(targetURL, targetCacheName) {
  const crusher = new cUseful.CrusherPluginCacheService().init ({
    store:CacheService.getUserCache()
  });
  Logger.log(targetURL);
  Logger.log(targetCacheName);
  var cached = crusher.get (targetCacheName);
  if (cached != null) {
    return cached;
  }
  // This fetch takes 20 seconds:
  var result = UrlFetchApp.fetch(targetURL);
  var contents = result.getContentText();
  crusher.put (targetCacheName, contents);
  return contents;
}

the line "rgMyRange.setValues(crewNames);" doesn't work because I only have a 1-dimensional array. But I ultimately want to write crewNames to column A and crewURLs to column B, which I think I could do using a 2d array fairly easily. Trying to avoid building a loop to iterate across my data if I can. Is there an easy way (without building such a loop) to combine those two 1d arrays into a 2d array, or something I'm missing in parser that might do it automatically?

Upvotes: 1

Views: 657

Answers (1)

Tanaike
Tanaike

Reputation: 201513

  • In your script, crewNames and crewURLs are 1 dimensional array.
  • You want to put crewNames and crewURLs to the column "A" and "B" of the active sheet, respectively.

If my understanding is correct, how about this modification?

From:

ssActive = SpreadsheetApp.getActiveSpreadsheet();
rgMyRange = ssActive.getRange("A:A");
rgMyRange.setValues(crewNames);

To:

var values = crewNames.map(function(e, i) {return [e, crewURLs[i]]}); // Added
ssActive = SpreadsheetApp.getActiveSheet(); // Modified
rgMyRange = ssActive.getRange(1, 1, values.length, values[0].length); // Modified
rgMyRange.setValues(values); // Modified

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 2

Related Questions