Image

Get all images
Add an image from Google Drive
Pause an image

Get all images


function getAllImages() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const medias = Display.MediaService.get({
    accountId: accountId,
  }).rval;
  if (medias.totalNumEntries == 0) {
    Logger.log('Media does not exist.');
    return;
  }
  for (let i = 0; i < medias.values.length; i++){
    let mediaRecord = medias.values[i].mediaRecord;
    Logger.log('mediaType-> ' + mediaRecord.imageMedia.mediaType
      + ', mediaFileType-> ' + mediaRecord.imageMedia.mediaFileType
      + ', mediaId-> ' + mediaRecord.mediaId
      + ', mediaName-> ' + mediaRecord.mediaName
      + ', mediaTitle-> ' + mediaRecord.mediaTitle);
  }
}

Add an image from Google Drive


function addImageFromGoogleDrive() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const fileId = 'fileIdAAA';
  const fileBytes = DriveApp.getFileById(fileId).getBlob().getBytes();
  const encoded = Utilities.base64Encode(fileBytes);
  const mediaArray = [
    {
      accountId: accountId,
      imageMedia: {
        data: encoded,
      },
      mediaName: 'mediaNameAAA.png',
      mediaTitle: 'mediaTitleAAA',
      userStatus: 'ACTIVE',
    }
  ];//Only 1 object can be set
  const medias = Display.MediaService.add({
    accountId: accountId,
    operand: mediaArray,
  }).rval;
  if (medias.values[0].operationSucceeded) {
    let mediaRecord = medias.values[0].mediaRecord;
    Logger.log('mediaId -> ' + mediaRecord.mediaId
      + ', mediaName -> ' + mediaRecord.mediaName
      + ', mediaTitle -> ' + mediaRecord.mediaTitle + ' have been added.');
  } else {
    Logger.log('mediaName-> ' + mediaArray[0].mediaName
      + ', mediaTitle-> ' + mediaArray[0].mediaTitle + ' could not to be added.');
  }
}

Pause an image


function pauseImage() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const mediaIds = [111111111, 22222222, 33333333];//Empty when not specified
  let mediaArray = [];
  for (let i = 0; i < mediaIds.length; i++){
    let mediaRecord = {
      accountId: accountId,
      mediaId: mediaIds[i],
      userStatus: 'PAUSED'
    };
    mediaArray.push(mediaRecord);
  }
  const mediasSet = Display.MediaService.set({
    accountId: accountId,
    operand: mediaArray,
  }).rval;
  for (let i = 0; i < mediasSet.values.length; i++){
    if (mediasSet.values[i].operationSucceeded) {
      let mediaRecord = mediasSet.values[i].mediaRecord;
      Logger.log('mediaId-> ' + mediaRecord.mediaId
        + ', mediaName-> ' + mediaRecord.mediaName
        + ', mediaTitle-> ' + mediaRecord.mediaTitle + ' stopped.');
    } else {
      let mediaRecord = mediaArray[i].mediaRecord;
      Logger.log('mediaId-> ' + mediaRecord.mediaId + ' could not stopped.');
    }
  }
}