逛到PTT的小說版,說需要自動使用google的表單再將資料轉成另一個sheet做紀錄。
以下為測試程式碼
幾點備忘事項
1.在試算表新增程式碼,並設定trigger,將form submit綁在myFunction上面。
2.Logger.log,可從檢視->log觀察此次執行的資料。
3.SpreadsheetApp.openById 從網址列取得試算表的ID。
4.換行為 \n
5.getRange從1開始,但轉成getValues是從0開始。
/**
* A trigger-driven function that sends out calendar invitations and a
* personalized Google Docs itinerary after a user responds to the form.
*
* @param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function myFunction(e) {
Logger.log('----------------->');
Logger.log(JSON.stringify(e));
Logger.log('----------------->');
var novel = e.namedValues['請輸入您想評價的小說書名。'][0];
var author = e.namedValues['請告訴我這本小說的作者是誰。'][0];
var category = e.namedValues['請選擇這部小說的分類。'][0];
var grade = e.namedValues['請選擇您對這部小說的評價。'][0];
var discuss = e.namedValues['請以文字簡單回答您對這部小說的看法。'][0];
//Logger.log(novel+","+author+","+category+"+"+grade+","+discuss);
OpenSheet(novel,author,category,grade,discuss);
}
function test(){
var novel = "半仙闖江湖";
var author = "作者";
var category = "網遊";
var grade = "3";
var discuss = "推推推";
OpenSheet(novel,author,category,grade,discuss);
}
function OpenSheet(novel,author,category,grade,discuss){
var ss = SpreadsheetApp.openById("***************2pk5LvU2F4yqM");//此處輸入要開啟的excel id
Logger.log(ss.getName());
var sheetCategory = ss.getSheetByName(category);
var lastRow = sheetCategory.getLastRow();
var maxCol = sheetCategory.getMaxColumns();
var arrRange = sheetCategory.getRange(3, 1, lastRow,2);
//Logger.log(maxCol);
var arrVal = arrRange.getValues();
// Logger.log(arrVal);
var repeat = false;
for(var i = 0;i<lastRow;i++)
{
Logger.log(arrVal[i][1]);
if(arrVal[i][1]==novel)
{
//評語
var update = sheetCategory.getRange((i+3),1,1,maxCol);
var arrUpdate = update.getValues();
for(var j = 9;j<maxCol;j++)
{
if(arrUpdate[0][j].toString()==""){
// var nowVal = update.getCell(1, j).getValue() + "\n"+discuss; // 同一個欄位斷行
update.getCell(1, (j+1)).setValue(discuss); //新的一欄
break;
}
}
//計算票數
var grassCount;
var grassCell;
switch (grade)
{
//毒草
case "1":
grassCell = update.getCell(1, 7);
break;
//普通草
case "2":
grassCell = update.getCell(1, 6);
break;
//仙草
case "3":
grassCell = update.getCell(1, 5);
break;
}
grassCount = grassCell.getValue();
grassCell.setValue(grassCount+1);
repeat = true;
}
if(arrVal[i][1].toString()==""){
lastRow = i+3;
break;
}
}
if(repeat==false){
var maxCol = sheetCategory.getMaxColumns()
var insert = sheetCategory.getRange(lastRow,1,1,10);
Logger.log("lastrow:"+lastRow+";"+insert.getValues());
insert.getCell(1,2).setValue(novel);
insert.getCell(1,3).setValue(author);
insert.getCell(1,4).setValue("謎");
switch (grade)
{
//毒草
case "1":
insert.getCell(1, 7).setValue(1);
insert.getCell(1, 6).setValue(0);
insert.getCell(1, 5).setValue(0);
break;
//普通草
case "2":
insert.getCell(1, 6).setValue(1);
insert.getCell(1, 7).setValue(0);
insert.getCell(1, 5).setValue(0);
break;
//仙草
case "3":
insert.getCell(1, 5).setValue(1);
insert.getCell(1, 6).setValue(0);
insert.getCell(1, 7).setValue(0);
break;
}
insert.getCell(1,8).setValue("本書的簡介是個謎"); //介紹
insert.getCell(1,9).setValue(discuss);
}
}
0 意見:
張貼留言