Transferring data to a different sheet based on specific criteria

I require help with my project. The ultimate objective is for a user to choose cells in a row and then utilize a button to transfer that data to another sheet. Upon uploading, I aim to match a specific cell and populate particular empty cells on that row. In case of no match, it should generate a new entry.

The concept revolves around multiple sheets utilized by the team, each containing similar categories with some cells filled out selectively. The goal is to amalgamate all the data into one sheet, eliminating the need to refer to various sheets for information retrieval. For instance, if product ID 123456 exists in the master sheet, uploading data with this product ID should append details such as weight, height, etc., to the corresponding cells on that row. On the other hand, when a user uploads Product ID 654321 not present in the master, a new line accommodating the selected data should be created, filling the appropriate cells accordingly.

In its current state, the script uploads everything to a master sheet without cross-checking the product ID. It keeps generating new entries every time the script runs. What I seek is to verify a specific cell's data - if found in the master, avoid adding the data in a new line; instead, fill the respective cells within that row.

As of now, the script is relatively straightforward:

function uploadData(){
 var css = SpreadsheetApp.getActiveSpreadsheet(); //set up current spreadsheet
 var csheet = css.getSheetByName("Sheet1") //set up current sheet
 var cdata = csheet.getActiveRange().getValues(); //retrieve selected data values
 var sRows = csheet.getActiveRange().getLastColumn(); //extract the value of the last column selected

 var mss = SpreadsheetApp.openById('1N5Orl2fQvmFmK63_y78V2k7jzBUYOjDxhixMSOCU7jI'); //access mastersheet for data addition
 var msheet = mss.getSheets()[0]; //fetch the sheet
 var mfindnextrow = msheet.getRange('A:A').getValues(); //obtain the values

 //this following code allows insertion of new data on an empty row
 var maxIndex = mfindnextrow.reduce(function(maxIndex, row, index) {
 return row[0] === "" ? maxIndex : index;
  }, 0); 

 //begin iterating through the row 
 cdata.forEach(function(row){
 msheet.setActiveRange(msheet.getRange(maxIndex + 2, 1, 1, sRows)).setValues(cdata); //insert the data into the mastersheet
  });
};    

Referencing the Uploading sheet: uploadsheet This sheet contains only one item with product code 1 I am seeking a solution where the system searches the master for a matching product code, appends the information if available, and creates a new entry with the specified product code if necessary.

Answer №1

Give this a try:

function transferSelectedData(){
  var sheet = SpreadsheetApp.getActive();
  var sourceSheet = sheet.getSheetByName("Sheet1"); 
  var selectedData = sourceSheet.getActiveRange().getValues();
  
  var targetSpreadsheet = SpreadsheetApp.openById('ssid');
  var targetSheet = targetSpreadsheet.getSheets()[0];
  
  targetSheet.getRange(targetSheet.getLastRow()+1, 1, selectedData.length, selectedData[0].length).setValues(selectedData);
}

Answer №2

The issue lies with the indexes specified in

msheet.getRange(maxIndex + 2, 1, 1, 1)

The final two numbers define the size of the range. When using 1,1, it restricts the range to just 1 row and 1 column, resulting in a single cell.

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

Interacting with user-generated data through data binding

My ultimate goal is to integrate user-created HTML content from a database into an Angular SPA with seamless navigation. One common approach to display HTML content retrieved from a backend is to utilize the DomSanitizer and bind it to the innerHTML proper ...

Check out the source code of the file causing the redirection

Whenever I click on a URL, it triggers a redirect using the window.location function. I am curious to examine the code within this HTML file. Unfortunately, as it is constantly redirecting, I am finding it difficult to access the source code. Is there a w ...

Issue with FabricJS: text function malfunctioning

I've recently started experimenting with FabricJS, but I'm having trouble getting text to display. Below is the code I'm using: var canvas = new fabric.Canvas('mainCanvas'); var str = new fabric.Text('Hello World', { ...

`After compilation, the Material UI component `theme.spacing` was not found in the definition

Currently, I am developing a text editor using Material-UI and draft.js, where the functionality is enclosed in Material-UI components. I have been comfortably working with version ~3.9, but for this specific project, I decided to upgrade to 4.0. Although ...

Ways to ensure the express route accesses my mocked module instead of the main one when making a request to it with supertest

Struggling to simulate an uploadImage middleware function. The challenge is as follows: When making a request with supertest to /users/me/avatar in the user route, I want it to utilize the mocked uploadImage instead of the original one. Question: Where s ...

Creating and organizing 3 interconnected node modules for optimal npm use

Currently, I am in the process of developing a node package called canto34, which was initially distributed as a single file named canto34.js. As the source code has expanded, it now consists of three distinct modules, each housed in its own file: canto ...

Shuffling specific table cells to exchange positions

I am attempting to create a script that will allow certain td elements (but not all) in different tr elements to switch places with the ones directly above or below them. My goal is to only target the last 3 td elements in each row, rather than the entire ...

What could be the reason for the statement "object instanceof Object is evaluated as

How to handle query information in Express on Node.js var query = request.query; Please see the information of query displayed below: Note: When running on mac: query instanceof Object returns true But when running on CentOS release 6.4 (Final): it ...

How to Accurately Obtain Serial Numbers in Add, Remove, and Clone Operations Using My Calculation Method

How To Properly Calculate SrNo in the Add, Remove, and Clone Functions By clicking "Add" multiple times, deleting rows, and then adding again, the SrNo becomes incorrect. I want it to calculate properly with my custom function... <div id="button_pro" ...

After a successful login, learn how to implement a bottom tab menu in React Native

I'm diving into the world of react-native and finding myself a bit lost when it comes to routing and navigation. I have 4 screens - Login, Register, Home, and Links. The Register and Login screens are all set up, with the ability to navigate back usin ...

What is the best way to apply a filter on two different multidimensional arrays?

After reviewing the other similar questions, I have yet to find a solution that fits my specific issue. What I'm looking for is to match the "impuesto" value in array "a" with the key in array "b". If they don't match, then remove those entries f ...

Issue with dropdown validation (button remains disabled)

I am working on creating a simple input form with validation requirements. Specifically, I want the button to be enabled or disabled based on the selection made in a dropdown menu. constructor(public fb: FormBuilder) { } /** * Form */ ...

Organizing elements in an array based on keys that incorporate logical operators

Wondering if there is a solution already available for this particular issue. I have an array structured as follows: [ "<=4/2" => 233 "16/8" => 73 ">16/8" => 122 "8/4" => 21 ] The task at hand is to sort this array in as ...

Is there a way to efficiently retrieve multiple values from an array and update data in a specific column using matching IDs?

In my Event Scheduler spreadsheet, I am looking for a way to efficiently manage adding or removing employees from the query table in column A. Currently, I have a dropdown list in each row to select names and a script that can only replace one name at a ...

What is the best way to retrieve the text from a linked button within my rad grid using jQuery?

I am facing an issue with a grid that contains link-buttons in the code column. The user can check multiple checkboxes, and when they click on the top button of my page, all selected codes will be displayed in a text box on another page using a jQuery func ...

Having trouble with the Keydown event - the image isn't moving as expected

Something seems off with my code. I have an image displayed on a canvas, but when I press the specified key, nothing happens. Can you help me figure out where I went wrong? let img = document.getElementById("ship"); let player = { x: 375, ...

Storing Iframe content without the need for a "save button" in a colorbox

I'm currently working on a small project that involves a main HTML page and a separate settings.html file. I've set it up so that the settings page pops up when clicked, using the following code: jQuery('#settings').colorbox({iframe:tr ...

Send the loop index as a parameter to a function within the onclick attribute of an HTML <input> element using vue.js

My goal was to develop a basic to-do list using vue.js to familiarize myself with it. Now, I've successfully added a remove button next to each item. To achieve this, I included a remove() function in JavaScript with a id parameter, which corresponds ...

Disqus comment box fails to appear on the screen

While going through the instructions provided on Disqus, I encountered an issue where the comment-box failed to render. Everything seemed to be correctly set up from the admin end on Disqus. var disqus_config = function () { this.page.url = {{request.b ...

What mechanisms do frameworks use to update the Document Object Model (DOM) without relying on a

After delving into the intricate workings of React's virtual DOM, I have come to comprehend a few key points: The virtual DOM maintains an in-memory representation of the actual DOM at all times When changes occur within the application or compo ...