You are on page 1of 2

BÀI TẬP CODE GOOGLE

Viết code Google Apps Script

Ví dụ gửi Email bằng Code Google:


function SendAnEmail() {
// Set the recipient email address
var email = 'ndsang@gmail.com'
// Create the email subject line.
var subject = 'This is my first script!';
// Create the email body.
var body = 'Hello, world!';
// Send an email
GmailApp.sendEmail(email, subject, body);
}

Ví dụ code Google bảng tính:


function myFunction() {
var myRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('A1');

var myValue = myRange.getValue();

myB1 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('B1');
myB1.setValue(myValue);

Logger.log('Giá trị trong B1 là: ' + myValue);


Logger.log('Something');
}

Code Google Văn bản


/**
* Creates a Google Doc and sends an email to the current user with a link to the doc.
*/
function createAndSendDocument() {
// Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Hello, world!');
// Access the body of the document, then add a paragraph.
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
// Get the URL of the document.
var url = doc.getUrl();
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Get the name of the document to use as an email subject line.
var subject = doc.getName();
// Append a new string to the "url" variable to use as an email body.
var body = 'Link to your doc: ' + url;
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body);
}

Ví dụ tạo hộp cảnh báo bằng Google code:


function hocggsheetBai8() {
var app = SpreadsheetApp;
var ss = app.getActive();
var ui = app.getUi();

// var nut = ui.alert('Nội dung',ui.ButtonSet.OK_CANCEL);


//
// if (nut == ui.Button.OK) {
// ui.alert('Đã xác nhận');
// } else {
// ui.alert('Đã hủy');
// }

var nut = ui.prompt('Nhập vào',ui.ButtonSet.YES_NO).getSelectedButton();

if (nut == ui.Button.YES) {
ui.alert('Bạn đã chọn Yes');
} else {
ui.alert('Đã chọn NO');
}
}

function prompt2() {
var app = SpreadsheetApp;
var ss = app.getActive();
var ui = app.getUi();

var text = ui.prompt('Sức khỏe hôm nay của bạn thế nào?',ui.ButtonSet.YES_NO).getResponseTex
t();

if (text == "không tốt") {


ui.alert('Đi khám bác sĩ nhé!');
} else {
ui.alert('Cứ tiếp tục nhé!');
}
}

Tham khảo:
https://hocggsheet.com/
https://drive.google.com/drive/u/0/folders/1wOD5aSLlBSgtN-3CAx3FrTO57vFbSbNe

You might also like