2017-04-18 162 views

回答

0

如何跟隨GAS樣本?如果你想運行除了GAS,請告訴我。

要使用此功能,首先請啓用Google高級Google服務和Google API控制檯的Google表格API第4版。

如何使用它如下。

  1. 在腳本編輯器,選擇資源>高級谷歌服務

  2. 在出現的對話框中,點擊谷歌API表的on/off開關。

  3. 在該對話框的底部,單擊Google API控制檯的鏈接。

  4. 在控制檯中,單擊過濾器框並鍵入API「Google Sheets API」的部分名稱,然後單擊該名稱即可看到它。

  5. 在下一個屏幕上,單擊啓用API。

  6. 關閉Developers Console並返回到腳本編輯器。在對話框中單擊確定。您啓用的高級服務現在可用於自動完成。

詳細信息是https://developers.google.com/apps-script/guides/services/advanced

腳本:

Sheets.Spreadsheets.create({ 
    "properties": 
    { 
    "title": "filename" // filename 
    }, 
    "sheets": 
    [ 
    { 
     "data": 
     [ 
     { 
      "startRow": 0, // 1st row 
      "startColumn": 7, // column h 
      "rowData": 
      [ 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h1" 
        } 
       } 
       ] 
      }, 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h2" 
        } 
       } 
       ] 
      }, 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h3" 
        } 
       } 
       ] 
      } 
      ] 
     } 
     ] 
    } 
    ] 
}); 

結果:

enter image description here

我爲做多JSON數據對不起。請更改示例文本。如果您想爲單元格使用數字,請使用"numberValue"而不是"stringValue"

如果我誤解了你的問題,我很抱歉。

新增1:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
var range = sheet.getRange('h1:h3'); 
var protection = range.protect().setDescription('protected'); 
var me = Session.getEffectiveUser(); 
protection.addEditor(me); 
protection.removeEditors(protection.getEditors()); 
if (protection.canDomainEdit()) { 
    protection.setDomainEdit(false); 
} 

上面的腳本保護h1:h3。所有者可以編輯所有單元格,但其他用戶不能編輯h1:h3

新增2:

Sheets.Spreadsheets.create({ 
    "properties": 
    { 
    "title": "filename" 
    }, 
    "sheets": 
    [ 
    { 
     "protectedRanges": 
     [ 
     { 
      "range": 
      { 
      "startColumnIndex": 7, 
      "endColumnIndex": 8, 
      "startRowIndex": 0, 
      "endRowIndex": 3, 
      "sheetId": 0 
      }, 
      "description": "protected", 
      "editors": 
      { 
      "users": 
      ["your e-mail address" 
      ] 
      } 
     } 
     ], 
     "data": 
     [ 
     { 
      "startColumn": 7, 
      "startRow": 0, 
      "rowData": 
      [ 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h1" 
        } 
       } 
       ] 
      }, 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h2" 
        } 
       } 
       ] 
      }, 
      { 
       "values": 
       [ 
       { 
        "userEnteredValue": 
        { 
        "stringValue": "sample text h3" 
        } 
       } 
       ] 
      } 
      ] 
     } 
     ], 
     "properties": 
     { 
     "sheetId": 0 
     } 
    } 
    ] 
}); 

該腳本創建新的電子表格。那時,它將數據添加到'h1:h3'並保護它們。請添加您的電子郵件地址作爲編輯器。這用於Google Sheet API v4。

+0

這真的很有幫助!非常感謝。你能不能讓我知道如何在創建表格時讓這三個值H1,H2,H3不可編輯? –

+0

您可以使用類保護。你可以在這裏看到細節。 https://developers.google.com/apps-script/reference/spreadsheet/protection – Tanaike

+0

我認爲這個參考文獻可以幫助我,但我無法找到這個SpreadsheetApp類的來源以及如何將它集成到上面的代碼中。你能幫我麼? –