Steps for Including Headers or Categories When Choosing Specific Ranges in Outcome

Is there a way to include headers like DATE, TOTAL, NAME, etc. when displaying categories? Currently, only the results are visible.

function showInputBox(){

 var ui = SpreadsheetApp.getUi();
 var input = ui.prompt("Please enter your rep name.",ui.ButtonSet.OK_CANCEL);

if(input.getSelectedButton() == ui.Button.OK){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Monitoring (Database)");
  var data = ws.getRange("A2:X" + ws.getLastRow()).getValues();
  var userSelectedRep = input.getResponseText().toLowerCase();
  var newData = data.filter(function(r){ return r[23] .toLowerCase() == userSelectedRep});
  var selectedColumns = newData.map(function (r) {
    return [r[10], r[9], r[1], r[17], r[18], r[19], r[20], r[21], r[22]];
  });

  if (newData.length > 0){
  var newWs = ss.insertSheet(userSelectedRep);
  newWs.getRange(3, 3, selectedColumns.length, selectedColumns[0].length).setValues(selectedColumns);
  } else {
    ui.alert ("No Matching data found for the entered name.");
  } 
  
  } else {
    ui.alert("Operation Canceled.");
  }
}

Even after changing getrange to A1:X, the outcome remains unchanged. Please refer to the screenshot below for more details.

Here is the current result image and I am looking to also display the header

Answer №1

Implementing Category Headers

To display categories, you can make use of the following code snippet which includes predefined headers and data to be posted in a specified range:

newWs.getRange(2, 3, 1, headers.length).setValues([headers]);
          
          newWs.getRange(3, 3, selectedColumns.length, selectedColumns[0].length).setValues(selectedColumns);

Example Script

function showInputBox() {
  var ui = SpreadsheetApp.getUi();
  var input = ui.prompt("Please enter your rep name.", ui.ButtonSet.OK_CANCEL);

  if (input.getSelectedButton() == ui.Button.OK) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var ws = ss.getSheetByName("Monitoring (Database)");
    var data = ws.getRange("A2:X" + ws.getLastRow()).getValues();
    var userSelectedRep = input.getResponseText().toLowerCase();
    var newData = data.filter(function (r) { return r[23].toLowerCase() == userSelectedRep });
    
    var selectedColumns = newData.map(function (r) {
      return [r[10], r[9], r[1], r[17], r[18], r[19], r[20], r[21], r[22]];
    });

    if (newData.length > 0) {
      var newWs = ss.insertSheet(userSelectedRep);
      
      // Define headers
      var headers = ["Header1", "Header2", "Header3", "Header4", "Header5", "Header6", "Header7", "Header8", "Header9"]; //modify as per requirement
      
      newWs.getRange(2, 3, 1, headers.length).setValues([headers]);
      
      newWs.getRange(3, 3, selectedColumns.length, selectedColumns[0].length).setValues(selectedColumns);
    } else {
      ui.alert("No Matching data found for the entered name.");
    }
  } else {
    ui.alert("Operation Canceled.");
  }
}

Source Sheet Examples

https://i.sstatic.net/xVtoTDKi.png

Generated Output Samples

https://i.sstatic.net/9Q4fndpK.png

Further details: addValues()

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

How can I address multiple buttons with various events using jQuery?

I am new to learning jQuery and I'm currently working on some exercises. However, I've run into an issue with two buttons in the DOM that are supposed to perform different actions. I can't seem to figure out how to assign different functions ...

Is it possible to asynchronously access a JSON object that has been retrieved from a local file on a global scale using the XMLHttpRequest method and

Having some trouble manipulating data from a local JSON file using this technique (no jQuery) as I can't seem to access the object on a global scale: var actual_JSON; function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.o ...

AngularJS filtering with multiple conditions

My ng-repeat list is currently displaying a collection of objects with a filter applied to only show objects inserted today. Here is how it looks: <div class="row msf-row" ng-repeat="record in recordlist | filter: record.date = dateJson"> Whi ...

Tips for swapping out a page for a component

Consider a scenario where we have a blog page containing a div element with the class "content" that displays previews of various articles. The goal is to dynamically replace the content within the div element with a specific article. How can this be acco ...

Animate play in reverse with JavaScript upon mouse exit

I'm currently using a code that works really well, but there's one thing I'd like to improve. When the mouse leaves, the animation stops abruptly which doesn't provide a great user experience. I have a few ideas: How can I make the &a ...

Creating a banner image that scrolls while maintaining a fixed size

I'm looking to add a banner image with a scrolling effect as users navigate down the page, but I'm not sure what this technique is called. An example of what I'm trying to achieve can be seen on this site. As the user scrolls down, the res ...

Having Trouble Dividing Routes into Individual Files in Express Version 4.0

I need to redirect routes ending with /api to a file called manager.js, which will then route it to /me. For example, a request to /me should be redirected to /api/me. In Express 3.x, splitting routes into separate files was simple, but I'm facing ch ...

The code encountered an error: "execute is not defined."

Whenever I click the execute button, I encounter an error in my console stating "Uncaught ReferenceError: execute is not defined". During onclickng, I am dynamically creating an input box and a button. <!DOCTYPE html> <html lang="en=US"& ...

Enhancing functionality by updating a function to accept an object as input instead of individual key:value pairs in TypeScript

I'm currently tackling a challenge with a friend's icon library component, specifically with their set() function. The issue arises when I want to invoke two functions, namely setRandomColor() and setColor(), both intended to update two values w ...

I find myself struggling to maintain the context of `this` within the Backbone View

I am currently working on enhancing my typeahead feature with debounce functionality. So far, I have achieved some success with the following code. The code successfully debounces the request. However, I encountered an issue where when I define a functio ...

The list in Jquery UI Autocomplete is not updating correctly

Currently, I am using jQuery UI Autocomplete in conjunction with WebSockets to fetch and display a list of options. Each time a keystroke is detected on the input field (.keyup()), a call is made to retrieve the list. However, I have encountered an issue w ...

Caution: It is not possible to make updates on a component while inside the function body of another component, specifically in TouchableOpacity

I encountered this issue: Warning: Trying to update a component from within the function body of another component. Any suggestions on how to resolve this? Interestingly, the error disappears when I remove the touchable opacity element. ...

Is there a way I can invoke my function prior to the form being submitted?

For the last couple of days, I've been struggling to make this code function properly. My goal is to execute a function before submitting the form to verify if the class for each variable is consistent. You can access my code here. Any assistance you ...

Exploring variations in error handling for JavaScript promises in Node.js depending on whether the error is synchronous or asynchronous

Exploring the nuances of promise error handling for thrown errors and exceptions in the promise executor, particularly in comparison to reject, within a node.js context. Differences in handling between reject and throw/exceptions are evident. Some source ...

Creating Dynamic Tables with jQuery

I have written a code to fetch values using jQuery and Ajax. The data is coming fine but I am facing an issue with populating the rows in my table. The loop doesn't seem to work properly. Here is my HTML table code: <div class="panel-body"> ...

JavaScript code for submitting form input values

Recently, I encountered an issue on my PHP page where I handle password change requests. I am struggling to implement a Javascript function that checks if the new password contains at least one special character before proceeding to update the database fie ...

Vue messaging application fails to display data upon mounting

Recently, I've been experimenting with different Vue chat libraries and encountered this interesting piece of code: <template> <p>{{ this.users[0] }}</p> </template> <script> export default { data() { return ...

What is the method for starting the JavaScript debugger in Google Chrome?

I'm looking to debug some JavaScript code in Google Chrome. Can anyone guide me on how to do that effectively? ...

Adding Rows to a DataTables Table

I am currently working on dynamically adding <tr/> tags to a DataTable, but I am struggling to find detailed documentation on how the process of "adding TR" is meant to be executed. Below is the setup of my DataTable: $("#Grid").DataTable({ state ...

Implementing automatic updates for Vue.js model data from a global object

I'm new to working with Vuejs and I'm encountering an issue with updating data from a global object automatically. My project involves the use of ES6 and Vuejs. The problem I'm facing is related to enabling/disabling a button based on a glo ...