简体   繁体   中英

Make a copy of google sheet into google drive folder

I have two Spreadsheets. The first spreadsheet contains my raw data that indicates the employee # and name of employees. The second spreadsheet is the spreadsheet I want to copy into a google drive folder. I want to update the specific fields on the 2nd spreadsheet based on the employee number and employee name from the 1st spreadsheet. Everytime it updates the cells in the second spreadsheet, it will create copy of the 2nd spreadsheet into google drive folder.

However, it keeps on setting just one value inside the replicated spreadsheets. It doesn't loop the employee names and employee numbers from the 1st spreadsheet.

My code is already replicating the 2nd spreadsheet. It's just the values aren't updating.

 function replicateCards() {
  var ss = SpreadsheetApp.openById('xxxxxxxx');
  var copyCard = SpreadsheetApp.openById('zzzzzzzzz');
  var getID = DriveApp.getFileById(copyCard.getId())
  var card = copyCard.getSheetByName("Card");
  var mastersheet = ss.getSheetByName("Mastersheet");
  var getLastRow = mastersheet.getLastRow();
  var destinationFolder = DriveApp.getFolderById('yyyyyyyyyy');
  ;
  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 employeeNumber = mastersheet.getRange(i + 1, 1).getValue();
    var employeeName = mastersheet.getRange(i + 1, 2).getValue();
    card.getRange("C3").setValue(employeeName);
    card.getRange("H3").setValue(employeeNumber);
    card.setActiveRangeList(changeColorToGrayList).setBackground("gray");
    card.setActiveRangeList(setValueToZero).setValue(0);
    //    var getID = DriveApp.getFileById(card).getId(); 
    getID.makeCopy(employeeNumber + " High Flyer Card", destinationFolder);
  }
}

I expect the output of getID.makeCopy(employeeNumber + " High Flyer Card", destinationFolder); contains different employee # and employee names, not just one value inside the google folder.

Your code should work as intended, unless a bulky file makes the code overlap.

If so, implementation of flush() will make your code run sequentially, see here for a detailed explanation.

In your case, modifying the for loop to

  for (i = 1; i < getLastRow; i++) {
    var employeeNumber = mastersheet.getRange(i + 1, 1).getValue();
    var employeeName = mastersheet.getRange(i + 1, 2).getValue();
    card.getRange("C3").setValue(employeeName);
    card.getRange("H3").setValue(employeeNumber);
    card.setActiveRangeList(changeColorToGrayList).setBackground("gray");
    card.setActiveRangeList(setValueToZero).setValue(0);
    getID.makeCopy(employeeNumber + " High Flyer Card", destinationFolder);
    SpreadsheetApp.flush();  
  }

should solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM