2017-04-18 155 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类的来源以及如何将它集成到上面的代码中。你能帮我么? –