Write application script code to export google sheet file data with 4 columns and many rows to sample file in cell A5 in sheet 3 then save the new file and open the file
Sure! Here's a Google Apps Script that you can use to export data from a Google Sheet to a new sheet and save it as a new file. Let's dive into the code!
function exportDataToNewSheet() {
// Open the Google Sheet file (replace 'YOUR_SHEET_ID' with actual Sheet ID)
const spreadsheet = SpreadsheetApp.openById('YOUR_SHEET_ID');
// Get the source sheet and the target sheet
const sourceSheet = spreadsheet.getSheets()[0]; // Assuming data is in the first sheet
const targetSheet = spreadsheet.getSheets()[2]; // Assuming you want to paste data into the third sheet
// Get the range of data (adjust as necessary)
const sourceRange = sourceSheet.getRange('A1:D100'); // For example, 4 columns and 100 rows
const data = sourceRange.getValues();
// Destination cell
const destinationRange = targetSheet.getRange('A5'); // Starting from cell A5
// Add data to the target sheet
targetSheet.getRange(destinationRange.getRow(), destinationRange.getColumn(), data.length, data[0].length).setValues(data);
// Save the new file
const newSpreadsheet = SpreadsheetApp.create('Exported Data');
const newSheet = newSpreadsheet.getSheets()[0];
newSheet.getRange(1, 1, data.length, data[0].length).setValues(data);
// Log the URL of the new file
Logger.log('New Spreadsheet URL: ' + newSpreadsheet.getUrl());
// Open the new file
const url = newSpreadsheet.getUrl();
const html = `<script>window.open('${url}', '_blank');</script>`;
const ui = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(ui, 'Opening New File');
}
'YOUR_SHEET_ID'
with your actual Google Sheet ID.A1:D100
indicates data with 4 columns and up to 100 rows, adjust this range as needed.If you’re thrilled with how easy that was and crave more such tech magic, visit trituenhantao.io. They’ve got a treasure trove of AI goodness waiting for you! 🚀