Freeze the heading rows trên google sheet

    1. 1. Thiết lập google sheet
    2. 2. Thiết lập Google Script
    3. 3. Cấp quyền cho Google Script
    4. 4. Lấy URL ứng dụng Google

Việc Thiết lập Google Sheet này nhằm mục đích lấy link hỗ trợ để thực hiện cấu hình việc tải danh sách khách hàng đã thu thập được lên trang tính của Google Sheet. Các thao Thiết lập Google SheetLấy link ứng dụng thực hiện như sau:

1. Thiết lập google sheet

Đầu tiên cần tạo 1 trang Google Sheets. (Nếu đã có thì bỏ qua bước này)

  • Truy cập đường dẫn: https://docs.google.com/spreadsheets/ sẽ mở trang Google Sheet giao diện như bên dưới sau đó chọn Blank
  • Tạo 4 trường HovaTen, Email, Phone, Date (Chú ý nhập chính xác, Không dấu, Không dấu cách)

Freeze the heading rows trên google sheet

2. Thiết lập Google Script

  • Sau khi đã có 1 trang Google Sheet chúng ta sẽ viết Script để ghi dữ liệu. Trên thanh công cụ chọn Công cụ -> Trình chỉnh sửa tập hợp lệnh
  • Trình duyệt sẽ mở 1 tab Google Script như hình dưới đây:

Freeze the heading rows trên google sheet

Thay đoạn code trong phần Mã.gs thành nội dung sau.

// Usage
//  1. Enter sheet name where data is to be written below
        var SHEET_NAME = "Trang tính2";

         //  2. Run > setup
//
//  3. Publish > Deploy as web app 
//    - enter Project Version name and click 'Save New Version' 
//    - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously) 
//
//  4. Copy the 'Current web app URL' and post this in your form/script action 
//
//  5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

// If you don't want to expose either GET or POST methods you can comment out the appropriate function


function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) {
  // shortly after my original solution Google announced the LockService[1]
  // this prevents concurrent access overwritting data
  // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
  // we want a public lock, one that locks for all invocations
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.

     try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);

         // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = []; 
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

Chú ý: Biến SHEET_NAME chính là tên của sheet bên trang google sheet, mặc định khi tạo mới sẽ là "Trang tính2" hoặc “sheet1”

Sau đó lưu lại (Hình dưới)

Freeze the heading rows trên google sheet


3. Cấp quyền cho Google Script

  • Chọn Chạy -> chạy chức năng Setup

Freeze the heading rows trên google sheet

  • Trình duyệt sẽ mở 1 cửa sổ mới, chọn vào tài khoản mình sẽ cấp quyền:

Freeze the heading rows trên google sheet

Freeze the heading rows trên google sheet

Freeze the heading rows trên google sheet

Freeze the heading rows trên google sheet

Freeze the heading rows trên google sheet

4. Lấy URL ứng dụng Google

  • Chọn Triển khai -> Tùy chọn triển khai mới

Freeze the heading rows trên google sheet

  • Chọn Cấu hình -> Ứng dụng web

Freeze the heading rows trên google sheet

  • Trong cửa sổ Tùy chọn triển khai mới, nhập đầy đủ thông tin sau đó bấm Triển khai

Freeze the heading rows trên google sheet

  • Sau khi bấm Triển khai. Cửa sổ tiếp theo mở ra (Hình dưới), Sau chép URL để tiến hành bước Tải danh sách khách hàng lên Google Sheet

Freeze the heading rows trên google sheet

Thông tin này có hữu ích không? Có Không

Nhận thêm hỗ trợ

Bạn cần thêm sự trợ giúp, hãy gửi yêu cầu cho Bizfly nhé!