0
votes

Here are my 2 spreadsheets:

Gsheet1: Googlesheet for replication (only employee # and employee name would change)

Gsheet2: Config sheet (contains the employee #, employee name, replication status, pubHTML link) that would feed employee # and employee name on Gsheet1

So I have this code that have these functionalities below:

  1. Replicate Gsheet1 depending on the number of employees (I currently have 800+ employees) on Gsheet2 and dump it to a google drive folder.
  2. after replication, will set a "completed" replication status to Gsheet2.
  3. Change the revision properties of the replicated sheet and make it published to web.
  4. Get the link of the published web (pubhtml) and put it on Gsheet2 pubHTML link column.

What happens is when I try to logger.log the results of this code below assuming that I have 2 records on my Gsheet2, it loops three times, the first and third record in loop are the same.

  var TemplatesFromDrive = DriveApp.getFolderById(SpreadsheetApp.getActive().getSheetByName("Master Config").getRange("B2").getValue()).getFiles();
  while (TemplatesFromDrive.hasNext()) {
    var File = TemplatesFromDrive.next();
    Logger.log(File.getName());

I was thinking if it's because of the Spreadsheet.flush() that I'm missing. Where is the best place where I can put it on my code so my loop will work properly? Below is my full code.

function replicateCards() {
    var ss = SpreadsheetApp.openById('Gsheet2-xxxx');
    var copyCard = SpreadsheetApp.openById('Gsheet1-xxxx');
    var getID = DriveApp.getFileById(copyCard.getId())
    var card = copyCard.getSheetByName("Card");
    var mastersheet = ss.getSheetByName("Mastersheet");
    var employeeNumber2 = ss.getRange("A2:A").getValues;
    var getLastRow = mastersheet.getLastRow();
    var destinationFolder = DriveApp.getFolderById('googledrivefolder-xxx');
    var changeColorToGrayList = card.getRangeList(['C7', 'E7', 'G7', 'I7', 'K7', 'M7', 'O7', 'Q7',
        'C9', 'E9', 'G9', 'I9', 'K9', 'M9', 'O9', 'Q9',
        'C11', 'E11', 'G11', 'I11', 'K11', 'M11', 'O11', 'Q11']);
    var setValueToZero = card.getRangeList(['C8', 'E8', 'G8', 'I8', 'K8', 'M8', 'O8', 'Q8',
        'C10', 'E10', 'G10', 'I10', 'K10', 'M10', 'O10', 'Q10',
        'C12', 'E12', 'G12', 'I12', 'K12', 'M12', 'O12', 'Q12']);
    for (i = 1; i < getLastRow; i++) {
        var badgeStatus = mastersheet.getRange(i + 1, 5).getValue();
        if (badgeStatus == "") {
            var employeeNumber = mastersheet.getRange(i + 1, 1).getValue();
            var employeeName = mastersheet.getRange(i + 1, 2).getValue();
            copyCard.getRange("H3").setValue(employeeNumber);
            copyCard.getRange("C3").setValue(employeeName);
          SpreadsheetApp.flush();
            getID.makeCopy(employeeNumber, destinationFolder);
            mastersheet.getRange(1 + i, 5).setValue("completed");
          SpreadsheetApp.flush();
            var files = DriveApp.getFolderById(SpreadsheetApp.openById("Gsheet1-xxxx").getSheetByName("Config Sheet").getRange("B1").getValue()).getFiles();
            while (files.hasNext()) {
                var file = files.next();
              Logger.log(file.getName());
                var Found = false;
                for (var j = 0; j < employeeNumber2.length; i++) {
                    if (employeeNumber2[j][0] == file.getName()) {
                        Found = true;
                    }
                }
                if (Found) {
                    continue;
                }
                try {
                    var fileId = file.getId();
                    var fileName = file.getName();
                    var revisions = Drive.Revisions.list(fileId);
                    var lastRevisionId = revisions.items[revisions.items.length - 1].id;
                    // get the resource and set the publish parameters
                    var resource = Drive.Revisions.get(fileId, lastRevisionId);
                    //       Logger.log(resource);
                    resource.published = true;
                    resource.publishAuto = true;
                    resource.publishedOutsideDomain = true;
                    // publish to the web
                    Drive.Revisions.update(resource, fileId, lastRevisionId);
                  SpreadsheetApp.flush();
                    var openByID = SpreadsheetApp.openById(fileId);
                  SpreadsheetApp.flush();
                    var googleDriveSheet = openByID.getUrl().replace("edit", "pubhtml"); // or replace("edit", "pub");
                  SpreadsheetApp.flush();
                    mastersheet.getRange(1 + j, 9).setValue(googleDriveSheet);
                  SpreadsheetApp.flush();

                } catch (err) {
                    Logger.log(err);
                }
            }
        }
    }
}
1
You shouldn't abuse the use of flush(), I would put only one at the end of the while or the for. Try that and see how it goes. Do you experience any issue without any flush()?Jescanellas

1 Answers

1
votes

Change var getLastRow = mastersheet.getLastRow(); to var getLastRow = mastersheet.getLastRow()-1;

The reason why is because you've started to get your data from the second row. If you would start at the third, you should do var getLastRow = mastersheet.getLastRow()-2; ...