JavaScript for listing all attributes

Here is a JavaScript that will list all attributes (key and values) for all selected listings

Neal

// <https://manual.iwascoding.com/gs8/en/Custom_JavaScript_Actions-Running_Scripts.html>

const newLine = "";  // "\n"
const blankLine = "\n";  // "\n\n"

function run() {
    var errors = [];
    var errorsCount = 0;
    for (const listing of selectedListings) {
      var validateErrors = validateListingProperties(listing);
      if (validateErrors.length > 0) {
          errorsCount++;
          var listingTitle = "";
          if (listing.title) {
              listingTitle = listing.title;
          }
          errors.push(blankLine + "Title: " + listingTitle + ": " + newLine);
          errors.push(... validateErrors);
      }
    }
    consoleLog("selectedListings: " + selectedListings.length);
    consoleLog("validateListing errors: " + errorsCount + newLine + errors + newLine);
}

function validateListing(listing) {
    var errors = [];
    var validateErrors = validateListingProperties(listing);
    errors.push(... validateErrors);
    return errors;
}

function validateListingProperties(listing) {
    var errors = [];
    
    const listingTitleLengthMinimum = 2;
    const listingTitleLengthMaximum = 80;
    var listingTitle = "";
    if (listing.title && (listing.title.length > listingTitleLengthMinimum) && (listing.title.length <= listingTitleLengthMaximum)) {
        listingTitle = listing.title;
    }
    
    if (!listing.attributes) {
        errors.push("No attributes" + newLine);
    }
    
    if (listing.attributes) {
        consoleLog("title: " + listing.title);
        var logHeader = true;
        const attributes = listing.attributes;
        for (const key in attributes) {  // use in, not of
            // consoleLog("key: " + key);
            if (!key || (key.length <= 0)) {
                errors.push("No key: " + key + newLine);
                continue;  // for loop
            }
            const values = attributes[key];
            if (!values || (values.length <= 0)) {
                errors.push("No values" + newLine);
                continue;  // for loop
            }
            for (const value of values) {
                // consoleLog("value: " + value);
            }
            if (logHeader) {
                logHeader = false;
                consoleLog("key, values: ");
            }
            consoleLog(key + ", " + values);
            errors.push(" Key: " + key + ", Values: " + values + newLine);
        }
    }

    // consoleLog("validateListingProperties: " + errors);
    return errors;
}

1 Like

A dumb question. Where do I put a JavaScript?

Select “Show Script Editor Window” from the Window menu.
Add a new script and paste it in. Then click the Run button in the lower right of the window.

https://manual.iwascoding.com/gs8/en/Custom_JavaScript_Actions-Running_Scripts.html

Neal

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.