2017-06-20 85 views
0

我是一位經驗豐富的程序員,對使用Google雲端硬盤的腳本編輯器沒有經驗。 由於我需要撰寫一些報告,因此我想知道如何利用Google雲端硬盤的腳本功能來簡化我的過程。通過從Google表格獲取數據創建Google文檔。

所以我的目標是我在單詞中創建了這種格式,對於單詞的某些部分,我需要輸入每個學生的分數。但是,由於手動操作非常苛刻,我想知道如何使用谷歌表格和谷歌文檔。

所以我想知道是否有一種方法可以讓我從電子表格中獲取某些數據(每個文檔一列),並將這些數字放在Google文檔的相應空間中,然後將其保存在Google Drive或發送它作爲一個電子郵件。然後,我將爲電子表格中的每個列重複此過程,直到創建所有人的報告。

如果你的專業程序員可以幫助我在這裏,將深表感謝。我從來沒有任何經驗與谷歌腳本編輯器,我不知道從哪裏開始。謝謝!

+0

歡迎來到StackOverflow,請參加[旅遊]。要求提供教程,提供甚至只是推薦的問題在StackOverflow上是無關緊要的。 – Yunnosch

+0

你是一個有經驗的程序員,你不知道從哪裏開始?你看過[參考](https://developers.google.com/apps-script/reference/spreadsheet/) –

回答

0

你可以查看這個Script for generating Google documents from Google spreadsheet data source教程。

/** 
* Generate Google Docs based on a template document and data incoming from a Google Spreadsheet 
* 
* License: MIT 
* 
* Copyright 2013 Mikko Ohtamaa, http://opensourcehacker.com 
*/ 

// Row number from where to fill in the data (starts as 1 = first row) 
var CUSTOMER_ID = 1; 

// Google Doc id from the document template 
// (Get ids from the URL) 
var SOURCE_TEMPLATE = "xxx"; 

// In which spreadsheet we have all the customer data 
var CUSTOMER_SPREADSHEET = "yyy"; 

// In which Google Drive we toss the target documents 
var TARGET_FOLDER = "zzz"; 

/** 
* Return spreadsheet row content as JS array. 
* 
* Note: We assume the row ends when we encounter 
* the first empty cell. This might not be 
* sometimes the desired behavior. 
* 
* Rows start at 1, not zero based!!! 
* 
*/ 
function getRowAsArray(sheet, row) { 
    var dataRange = sheet.getRange(row, 1, 1, 99); 
    var data = dataRange.getValues(); 
    var columns = []; 

    for (i in data) { 
    var row = data[i]; 

    Logger.log("Got row", row); 

    for(var l=0; l<99; l++) { 
     var col = row[l]; 
     // First empty column interrupts 
     if(!col) { 
      break; 
     } 

     columns.push(col); 
    } 
    } 

    return columns; 
} 

/** 
* Duplicates a Google Apps doc 
* 
* @return a new document with a given name from the orignal 
*/ 
function createDuplicateDocument(sourceId, name) { 
    var source = DocsList.getFileById(sourceId); 
    var newFile = source.makeCopy(name); 

    var targetFolder = DocsList.getFolderById(TARGET_FOLDER); 
    newFile.addToFolder(targetFolder); 

    return DocumentApp.openById(newFile.getId()); 
} 

/** 
* Search a paragraph in the document and replaces it with the generated text 
*/ 
function replaceParagraph(doc, keyword, newText) { 
    var ps = doc.getParagraphs(); 
    for(var i=0; i<ps.length; i++) { 
    var p = ps[i]; 
    var text = p.getText(); 

    if(text.indexOf(keyword) >= 0) { 
     p.setText(newText); 
     p.setBold(false); 
    } 
    } 
} 

/** 
* Script entry point 
*/ 
function generateCustomerContract() { 

    var data = SpreadsheetApp.openById(CUSTOMER_SPREADSHEET); 

    // XXX: Cannot be accessed when run in the script editor? 
    // WHYYYYYYYYY? Asking one number, too complex? 
    //var CUSTOMER_ID = Browser.inputBox("Enter customer number in the spreadsheet", Browser.Buttons.OK_CANCEL); 
    if(!CUSTOMER_ID) { 
     return; 
    } 

    // Fetch variable names 
    // they are column names in the spreadsheet 
    var sheet = data.getSheets()[0]; 
    var columns = getRowAsArray(sheet, 1); 

    Logger.log("Processing columns:" + columns); 

    var customerData = getRowAsArray(sheet, CUSTOMER_ID); 
    Logger.log("Processing data:" + customerData); 

    // Assume first column holds the name of the customer 
    var customerName = customerData[0]; 

    var target = createDuplicateDocument(SOURCE_TEMPLATE, customerName + " agreement"); 

    Logger.log("Created new document:" + target.getId()); 

    for(var i=0; i<columns.length; i++) { 
     var key = columns[i] + ":"; 
     // We don't replace the whole text, but leave the template text as a label 
     var text = customerData[i] || ""; // No Javascript undefined 
     var value = key + " " + text; 
     replaceParagraph(target, key, value); 
    } 

} 

正如@詹姆斯唐納蘭說明,請有關如何使用它來創建腳本,訪問該服務的official documentation,並修改谷歌表格文件。

相關問題