Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
1
Indexable
Never
function main() {
  var spreadsheetUrl = 'https://docs.google.com/spreadsheets/d/10lAFnC81p6IMslxrOkqHjOPx-3wpPOo0RTkqqXCq1TY/edit?usp=sharing';
  var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
  var sheet = spreadsheet.getSheetByName('Sheet2');
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 1; i <= numRows - 1; i++) {
    var row = values[i];
    var customerId = row[0];
    var adGroupId = row[1];
    var adType = row[2];
    var label = row[3];
    var headlines = row.slice(4, 19);
    var descriptions = row.slice(19, 23);
    var path1 = row[23];
    var path2 = row[24];
    var finalUrl = row[25];

    // Check if finalUrl is undefined or empty
    if (!finalUrl) {
      Logger.log('Row ' + (i + 1) + ': finalUrl is undefined or empty');
      continue; // Skip this row and go to the next one
    }

    // Check if any headlines or descriptions are too long
    for (var j = 0; j < headlines.length; j++) {
      if (headlines[j].length > 30) {
        Logger.log('Row ' + (i + 1) + ', Headline ' + (j + 1) + ': Text is too long');
        continue; // Skip this row and go to the next one
      }
    }
    for (var k = 0; k < descriptions.length; k++) {
      if (descriptions[k].length > 90) {
        Logger.log('Row ' + (i + 1) + ', Description ' + (k + 1) + ': Text is too long');
        continue; // Skip this row and go to the next one
      }
    }

    // Select the ad group to create the ad in
    var adGroupSelector = AdsApp.adGroups()
        .withIds([adGroupId]);

    var adGroupIterator = adGroupSelector.get();
    if (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();

      // Prepare the headlines and descriptions
      var responsiveSearchAdHeadlines = headlines.map(function(headline) {
        return {text: headline};
      });

      var responsiveSearchAdDescriptions = descriptions.map(function(description) {
        return {text: description};
      });

      // Create the responsive search ad
      adGroup.newAd().responsiveSearchAdBuilder()
          .withHeadlines(responsiveSearchAdHeadlines)
          .withDescriptions(responsiveSearchAdDescriptions)
          .withPath1(path1)
          .withPath2(path2)
          .withFinalUrl(finalUrl)
          .build();
    }
  }
}