Networks Business Online Việt Nam & International VH2

Apps script google sheet

Đăng ngày 08 November, 2022 bởi admin

Học Apps Script Google Sheet toàn tập

Apps Script Google sheet là gì?

App Script Google Script – là 1 ngôn ngữ lập trình dựa trên ngôn ngữ lập trình gốc là Javascript.  Với công cụ này bạn có thể lập trình để thao tác, can thiệp trực tiếp đến các dịch vụ của Google, giúp tự động hóa đơn các quá trình làm thủ công.

Apps Script Google có thể làm được những gì?

    Thêm menu, dialogs, và thanh sidebar tùy chỉnh vào Google Docs, Sheets và Forms.
    Viết các hàm mở rộng hoặc các macros cho Google Sheets.
    Xuất bản Web Apps – độc lập hoặc tích hợp vào trang web của Google Sites.
    Tương tác với các dịch vụ khác của Google, bao gồm AdSense, Analytics, Lịch, Drive, Gmail và Bản đồ.
    Xây dựng các tiện ích bổ sung để mở rộng Google Docs, Sheets, Slides và Forms và xuất bản chúng lên cửa hàng Add-on.
    Chuyển đổi ứng dụng Android thành một tiện ích bổ sung Android để ứng dụng có thể trao đổi dữ liệu với Google Doc hoặc Sheet của người dùng trên thiết bị di động.
    Xây dựng Chat bot cho Hangout chat

Hiện tại Apps Scripts Google Script có thể lập trình để thao tác với hầu hết các dịch vụ của Google:

    Calendar (Lịch)
    Contacts (Danh bạ)
    Documents (Tài liệu)
    Drive (Lưu trữ đám mây)
    Forms (Biểu mẫu)
    Gmail (Email)
    Group (Nhóm)
    Language (Dịch)
    Maps (Bản đồ)
    Sites (Trang web)
    Slides (Trình chiếu)
    SpreadSheet (Bảng tính).

Sau đây mình gởi đến các bạn các đoạn code Apps Scripts Google Script cơ bản nhé:

1. Khai báo biến
–   Biến kiễu chữ:
  var tenbien = Giá trị;
 Ví dụ:
 var hoten=”Tran Van A”;
Logger.log(hoten);

– Biến kiễu số
var so=10;
so=so+1;
Logger.log(so);

Bạn đang đọc: Apps script google sheet

2. Tạo biến Sheet

var sheet = SpreadsheetApp.getActiveSheet(); //lấy đối tượng sheet đưa vào biến để xử lý
  spreadsheet.getRange(‘B5’).activate();   // Kích hoạt vị trí chọn
  spreadsheet.getCurrentCell().setValue(‘4’); // Gán giá trị cho ô đang chọn
  Logger.log(‘Noi dung in’); //Ghi nội dung log, chỉ xem được tại trình soạn lệnh

// HÀM IN NỘI DUNG RA LOG

3. Vòng lặp
  var sheet = SpreadsheetApp.getActiveSheet(); // lấy sheet hiện tại được vào biến
  var data = sheet.getDataRange().getValues(); //lấy giá trị hàng (trong gồm các ô) đưa vào biến mãng
  for (var i = 0; i < data.length; i++) {  // lặp
    Logger.log(‘Product name: ‘ + data[i][0]); //in ra
    Logger.log(‘Product number: ‘ + data[i][1]);
  }

4. Chèn hàng vào google sheet

 var sheet = SpreadsheetApp.getActiveSheet();
  sheet.appendRow([‘Cotton Sweatshirt XL’, ‘css004’]);
 

5. Set font cho ô

 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange(‘B2:C2’);
  cell.setFontStyle(‘italic’);
 

6. Set DataValidation cho ô
// Set a rule for the cell B4 to be a number between 1 and 100.
  var cell = SpreadsheetApp.getActive().getRange(‘B4’);
  var rule = SpreadsheetApp.newDataValidation()
     .requireNumberBetween(1, 100)
     .setAllowInvalid(false)
     .setHelpText(‘Number must be between 1 and 100.’)
     .build();
  cell.setDataValidation(rule);

//=============================
7. Hiển thị thông báo:
var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     ‘Please confirm’,
     ‘Are you sure you want to continue?’,
      ui.ButtonSet.YES_NO);

  // Process the user’s response.
  if (result == ui.Button.YES) {
    // User clicked “Yes”.
    ui.alert(‘Confirmation received.’);
  } else {
    // User clicked “No” or X in the title bar.
    ui.alert(‘Permission denied.’);
  }
 

8. Hiển thị hộp thoại có ô nhập liệu

var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
      ‘Let\’s get to know each other!’,
      ‘Please enter your name:’,
      ui.ButtonSet.OK_CANCEL);

  // Process the user’s response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked “OK”.
    ui.alert(‘Your name is ‘ + text + ‘.’);
  } else if (button == ui.Button.CANCEL) {
    // User clicked “Cancel”.
    ui.alert(‘I didn\’t get your name.’);
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert(‘You closed the dialog.’);
  }

 

9. HÀM GỞI MAIL
function SendAnEmail() {
  // gán email nhận
  var email = ‘[email protected]
  // Tiều đề gởi mail
  var subject = ‘This is my first script!’;
  // Nội dung gởi mail
  var body = ‘Hello, world!’;
  // Send an email
  GmailApp.sendEmail(email, subject, body);
}

Source: https://vh2.com.vn
Category : Tin Học