Câu hỏi:

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');
}
  1. Replace 'YOUR_SHEET_ID' with your actual Google Sheet ID.
  2. The script assumes that you're copying data from the first sheet of the spreadsheet to the third sheet.
  3. The range A1:D100 indicates data with 4 columns and up to 100 rows, adjust this range as needed.
  4. The data will be pasted starting from cell A5 in the third sheet of the original spreadsheet.
  5. The script also creates a new spreadsheet file with the copied data and opens it in a new browser tab.

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! 🚀


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử