Học Ms excelKỹ năng vi tính

Google sheet App Script Copy and paste value from One Spreadsheet to Other spreadsheet

Giáo án link (giaoanlink) chia sẻ đến các bạn project Google sheet App Script Copy and paste value from One Spreadsheet to Other spreadsheet. Trong project này chúng ta sẽ thực hiện việc copy giá trị của vùng dữ liệu của Spreadsheet này đến paste vào sheet của Spreadsheet khác. Việc thực thi lệnh có thể thông qua Menu custom hoặc các bạn tạo các triger đáp ứng nhu cầu.

Xem thêm các bài học excel ứng dụng khác:

Mã apps scrip trong project:

function onOpen(){
  let ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
  .addItem('Copy to other spreadsheet','runsies')
  .addToUi();
}

function runsies() {
  importRange(
    "1NlK7e8m8HHRxG6noCH0SrlEWA33uO9OBypSAW4ULuaM",  //Source Spreadsheet ID
    "Source!A1:D", // Source Range 
    "1Gr90_SuotOFZNQTzs5VyNHPKop3ajpkrAMEf0Ncp7sA", // Target Spreadsheet ID
    "Target!B2" // Target Begin Range
  );
};

/** 
 * sourceID - ID of Source Spreadsheet 
 * sourceRange - Contain Sheet tab and Range of data
 * targetID - ID of Target Spreadsheet
 *  targetRangeStart - cell address to start pasting the data area into targetsheet
 */
function importRange(sourceID, sourceRange, targetID, targetRangeStart){

  // Get the source range values
  const sourceSS = SpreadsheetApp.openById(sourceID);
  const sourceRng = sourceSS.getRange(sourceRange)
  const sourceVals = sourceRng.getValues();

  // Get the target sheet and cell location.
  const targetSS = SpreadsheetApp.openById(targetID);
  const targetStartRange = targetSS.getRange(targetRangeStart);
  const targetSheet = targetStartRange.getSheet();

  // Clear previous entries. 
  targetSheet.clear();

  // Get the full data range to paste from start range.
  const targetRange = targetSheet.getRange(
      targetStartRange.getRow(),
      targetStartRange.getColumn(),
      sourceVals.length,
      sourceVals[0].length
    );
  
  // Paste in the values.
  targetRange.setValues(sourceVals);

  SpreadsheetApp.flush();
};

Video hướng dẫn






giaoanppt

Giaoan.link trang chia sẽ giáo án điện tử, bài giảng powerpoint, template powerpoint, nguyên liệu làm bài giảng, tài liệu, biểu mẫu miễn phí!