Duplicate a Google Sheet and save it to a specific folder in Google Drive

I currently have two spreadsheets in my possession. The first spreadsheet consists of raw data that includes unique employee numbers and the names of the employees. The second spreadsheet is the one I aim to duplicate to a designated Google Drive folder. My goal is to update specific fields within the second spreadsheet based on the corresponding employee number and name provided in the first spreadsheet. Each time these changes are made, a new copy of the updated second spreadsheet will be generated in the Google Drive folder.

However, there seems to be an issue with only one value being set in the replicated spreadsheets. It does not iterate through the list of employee names and numbers present in the first spreadsheet as intended.

While my current code successfully replicates the second spreadsheet, it fails to properly update the values within the duplicated sheets.

 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);
  }
}

My expectation is that the output of

getID.makeCopy(employeeNumber + " High Flyer Card", destinationFolder);
should include varying employee numbers and names, rather than just a single value in the Google Drive folder.

Answer №1

If you're experiencing issues with your code overlapping due to a large file, consider incorporating the flush() method for sequential execution. For a more detailed explanation, check out this link.

In this scenario, adjusting the for loop like so:

  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();  
  }

can help resolve the issue at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with submitting a form through Ajax on Rails 4

Having models for advertisement and messages with forms that both utilize Ajax (remote => true) for submission. The message form submits perfectly, remains on the same page, and handles the response efficiently. However, the advertisement form fails to ...

Dynamic filtering with Javascript

I am searching for inspiration on how to create a filter in the left sidebar that dynamically updates the page content when clicked, and if there are subcategories, displays them below the selected filter in the sidebar. I've discovered that AJAX is ...

Avoiding the utilization of automatically generated JSON files as a data source in Rails when

I have implemented javascript code that uses JSON to generate a timeline. The JSON is being created using json_builder and it contains only the current user's information. The JSON displays correctly when accessed directly through its URL, but when th ...

Refining/searching with selectors in AJAX response

As someone who is new to javascript and coding in general, I am facing a challenge with filtering and manipulating data received through an AJAX request. Unfortunately, I do not have access to the server-side code. The server responds with rota information ...

JavaScript Thumbnail Slider Builder

I've been working hard on developing a custom JavaScript thumbnail slider that uses data-src. Everything seems to be in order, except the next and previous buttons are not functioning properly. Any assistance would be greatly appreciated. Here's ...

Experiencing difficulties with using the javascript alternative to jQuery.ajax()

Here is a JavaScript Object: const data = { name: "John", age: 30, } const jsonData = JSON.stringify(data); The AJAX Request below successfully passes the data: $(function(){ $.ajax({ url:'two.php', ty ...

Generating multiple div elements within an AJAX iteration

Currently, I am retrieving data from the server side using AJAX. My goal is to populate data from a list of objects into divs but I am facing an issue where I cannot create the div while inside the foreach loop. $(document).ready(function () { var ...

Combining several objects into a one-dimensional array

I am encountering a small issue with correctly passing the data. My form is coming in the format {comment:'this is my comment'} and the id is coming as a number. I need to send this data to the backend. let arr = []; let obj = {}; o ...

Creating JavaScript code using PHP

In my current project, there is a significant amount of JavaScript involved. I'm finding that simply generating basic strings and enclosing them within "<script>" tags may not be the most efficient approach. What are some alternative methods fo ...

Toggle class on child element when parent is clicked

I am currently working on a functional React component that looks like this: const RefreshButton = () => ( <IconButton> <RefreshIcon /> </IconButton> ) My goal is to dynamically assign a class attribute ...

The icons from FontAwesome in Vue do not update when computed

I am seeking a way to dynamically change the header icon based on a conversation property. <a class="navbar-item" :title="$t('header.lock')" @click="makePrivate"> <i class="fas" :class="getLockClass"></i> </a> These ...

The data-ng-bind directive cannot be used with the <option> tag

Currently, I am in the process of learning angular and encountered a problem that has me stuck. Upon researching on AngularJS : Why ng-bind is better than {{}} in angular?, I found out that both {{}} and ng-bind are said to produce the same result. However ...

d3.on event firing prior to mouseover event

This page showcases a dynamic d3 bar chart. On line 162, we have an interesting event listener that logs a message when the user hovers over a bar on the chart. However, the log is displayed as soon as the page loads. You can see the running code in actio ...

Tips for assigning a class name to a variable element within a react component?

I am interested in dynamically adding classes to an element. While I am familiar with methods using html-dom and passing a JavaScript expression to className, I am seeking a different approach. Is there a way to add classes similar to pushing them to an ar ...

Webpack 4 Failing to Properly Render Select Tag

I am encountering an issue with a basic select element that is not displaying correctly due to Webpack. The screenshot below illustrates the final rendered page source, where the closing tag for the select element has been incorrectly positioned before the ...

Utilizing Jquery and JavaScript to filter out specific HTML objects retrieved from an AJAX response

I'm encountering a puzzling issue with this snippet of HTML: <div id="1"> <div class="text"> Text for div 2 </div> <img src="images/image1.jpg"></img> </div> <div id="2"> <div class="text"> ...

What could be preventing me from successfully calling the JavaScript AJAX function in this particular situation?

Here is my code snippet from a smarty template: <form name="transaction_form" id="transaction_form"> <table class="trnsction_details" width="100%" cellpadding="5" > <tbody> <tr> ...

How can I utilize JavaScript to retrieve the background color of a table cell upon clicking?

I am trying to create a function where an alert pops up displaying the background color of a table cell whenever it is clicked. Unfortunately, I am having trouble accessing and retrieving the background color value. The table cell in question is defined a ...

Creating clickable div elements within a loop

In the .JS file I have the function below (which I later call in an HTML file): function writeDiv(){ x = 1 myArray.forEach(item => { htmlText += '<div class="divsArray">'; htmlText += '<h5> Value of X ...

JQuery HTML form validation continues to send emails even when there are blank entries

I've set up an HTML form with three required fields, and I'm trying to prevent the form from submitting an AJAX call if those fields are left empty. $("#contact").submit(function(e){ e.preventDefault(); var ajaxurl = '<?php echo ...