Technologies:
Tolerim
a month ago
My goal is to transfer a fixed column and rows relative to it to the last row of another tab/sheet within the same spreadsheet, using app script.
function moveData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const invoiceDb = ss.getSheetByName("Invoice_DB");
const invoiceDbLastRow = invoiceDb.getLastRow();
let lastRow = sheet.getLastRow();
let rangeValue = sheet.getSheetValues(5,1,lastRow,7);
let invoiceDbCounter = 1
for (var i = 5; i <= rangeValue.length; i++){
let value = sheet.getRange(i,1,1,7).getValues();
invoiceDb.getRange(invoiceDbLastRow + invoiceDbCounter,1,1,7).setValues(value);
}
}
Answers(2)
Vogi
a month ago
Verified Answer
Thank you for sharing your code snippet and providing a screenshot of your source and target sheets. Based on your code snippet, it appears that you are trying to move data from one sheet to another within the same spreadsheet while clearing all data from the source sheet using Google Apps Script.
I noticed that you have defined a variable named rangeValue which contains the values of a range of cells in your source sheet. However, I couldn't find where this variable is used in your code snippet.
To move data from one sheet to another within the same spreadsheet using Google Apps Script, you can use the setValues() method. This method sets a rectangular grid of values (two-dimensional array) starting at a given cell.
Here's an updated version of your code snippet that should work:
function moveData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const invoiceDb = ss.getSheetByName("Invoice_DB");
const invoiceDbLastRow = invoiceDb.getLastRow();
let lastRow = sheet.getLastRow();
let rangeValue = sheet.getSheetValues(5,1,lastRow,7);
let invoiceDbCounter = 1
for (var i = 0; i < rangeValue.length; i++){
invoiceDb.getRange(invoiceDbLastRow + invoiceDbCounter,1,1,7).setValues([rangeValue[i]]);
invoiceDbCounter++;
}
sheet.getRange(5,1,lastRow,7).clearContent();
}
In this updated version of your code snippet:
- The setValues() method is used to set the values of each row in rangeValue to the target sheet.
- The clearContent() method is used to clear all data from the source sheet.
I hope this helps! Let me know if you have any further questions or concerns.Sources
How to Copy a Row to another Sheet with Google Apps ScriptJump from one spreadsheet to another using Google App ScriptCopy or move rows to another sheet automaticallyGoogle Apps Script: Move data between spreadsheetsGoogle Apps Script - Copy data to different worksheet and appendExtending Google Sheets | Apps Script | Google DevelopersTolerim
a month ago
It looks like you are trying to move data from one sheet to another in a Google Spreadsheet using Google Apps Script. However, you mentioned that the range wasn't showing up in the target sheet.
One potential issue with your code is that you are starting your for loop at index 5, which may be skipping over the first 4 rows of data. Try starting your loop at index 1 instead:
for (var i = 1; i <= rangeValue.length; i++){
let value = sheet.getRange(i,1,1,7).getValues();
invoiceDb.getRange(invoiceDbLastRow + invoiceDbCounter,1,1,7).setValues(value);
invoiceDbCounter++;
}
Additionally, you mentioned that you want to clear the range from the initial sheet after moving the data. You can achieve this by using the clearContent() method on the range that you want to clear:
let range = sheet.getRange(5,1,lastRow,7);
range.clearContent();
Put that code after the for loop and it will clear the content of the range after the data has been moved to the other sheet.
Hope this helps! Let me know if you have any other questions.