Skip to content

Instantly share code, notes, and snippets.

@umjammer
Last active November 23, 2022 19:33
Show Gist options
  • Save umjammer/adf9af8ca43db135302e3a090a35a2d5 to your computer and use it in GitHub Desktop.
Save umjammer/adf9af8ca43db135302e3a090a35a2d5 to your computer and use it in GitHub Desktop.
[Google App Script] foreach

Google App Script foreach

Spread Sheet

function foreachSampleForSpreadSheet() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  data.forEach(function (row, y) {
    row.forEach (function (cell, x) {
      if (typeof(cell) === 'string') { 
        // WTF index!
        Logger.log(y + "," + x + ": " + cell + ", " + data[y][x] + ", " + sheet.getRange(y + 1, x + 1).getValue());
        //sheet.getRange(y + 1, x + 1).setValue("hello world");
      }
    });
  });
};

Google Drive

function foreachSampleForGoogleDrive() {
  var files = DriveApp.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment