arindam / gsheet-appscript
一个用于访问和管理任何 Google 表格的 Laravel 扩展包
v3.0.0
2024-03-10 18:07 UTC
This package is auto-updated.
Last update: 2024-09-10 19:14:29 UTC
README
一个用于访问和管理任何 Google 表格的 Laravel 扩展包。
安装
不依赖于 PHP 版本和 Laravel 版本
步骤 1: 运行 composer 命令
composer require arindam/gsheet-appscript
步骤 2: Laravel 无自动发现
如果你不使用自动发现,将 ServiceProvider 添加到 config/app.php 中的 providers 数组
在 providers 部分
Arindam\GsheetAppScript\GsheetAppScriptServiceProvider::class,
在 aliases 部分
'GsheetAppScript' => Arindam\GsheetAppScript\Gsheet\GsheetAppScriptClassFacade::class,
步骤 3: 发布包配置
php artisan vendor:publish --provider="Arindam\GsheetAppScript\GsheetAppScriptServiceProvider" --force -OR- php artisan vendor:publish --tag="gsheet-appscript:config"
步骤 4: 将 Google Sheet AppScript API URL 添加到 .env 文件
GSHEET_APPSCRIPT_API_URL=_YOUR_APP_SCRIPT_API_URL_
如何使用?非常简单
门面辅助函数
use GsheetAppScript; GsheetAppScript::allRecords(); //get all records from google sheet GsheetAppScript::addRow(['Text1', 'Text2', 'Text3' ...]); //add row in the google sheet GsheetAppScript::setHeading(['Heading1', 'Heading2', 'Heading3' ...]); //set or edit heading in the google sheet GsheetAppScript::editRow(['id' => 4, 'Text1', 'Text2', 'Text3' ...]); //edit data in the google sheet, just pass the row number as id with data GsheetAppScript::removeRow(['id' => 4]); //remove row from google sheet, just pass the row number GsheetAppScript::clearSheet(); //delete all records in google sheet
通过路由直接使用
- >>
只需安装并运行以下路由
Ex: http://your-website/onex/gsheet Ex: http://:8000/onex/gsheet
您可以在 "config/gsheet-appscript.php" 中修改配置设置
/** If you want to disable the route or this feature, then make it false */ 'is_route_enabled' => true,
/** If you want to change the route prefix */ 'route_prefix' => 'onex',
/** If you want to change the route name or path */ 'route_name' => 'gsheet',
/** If you want to change the page heading */ 'page_heading' => 'Google Sheet',
/** If you want to enable the securiry for access the google sheet information * Then make it ('is_enabled') true and also you can set login-id and password through .env */ 'authentication' => [ 'is_enabled' => env('GSHEET_APPSCRIPT_AUTH_ENABLED', false), 'login_id' => env('GSHEET_APPSCRIPT_LOGIN_ID', 'onexadmin'), 'password' => env('GSHEET_APPSCRIPT_LOGIN_PASSWORD', 'onexpassword') ]
Google AppScript 代码
只需将以下代码复制并粘贴到您的 Google 表格 AppScript 部分,并获取应用 URL
/** All Get Request */ function doGet() { return ContentService.createTextOutput(getAllRows()); } /** All Post Request */ function doPost(req) { let data = JSON.parse(req.postData.contents); if(data.actionkey == "SAVE") { return ContentService.createTextOutput(addSaveRow(data)); } if(data.actionkey == "UPDATE") { return ContentService.createTextOutput(updateSaveRow(data)); } if(data.actionkey == "DELETE") { return ContentService.createTextOutput(deleteRow(data)); } if(data.actionkey == "VIEW") { return ContentService.createTextOutput(viewRow(data)); } if(data.actionkey == "CLEAR") { return ContentService.createTextOutput(clearSheet()); } } /** Add */ function addSaveRow(jsonObjData) { if (jsonObjData['actionkey']) { delete jsonObjData['actionkey']; } SpreadsheetApp.getActiveSheet().appendRow(Object.values(jsonObjData)); return 'SUCCESS'; } /** Edit */ function getRowId(textId) { let findData = SpreadsheetApp.getActiveSheet().createTextFinder(textId).matchEntireCell(true).findNext(); if(findData) { return findData.getRow(); } return 0; } /** Update */ function updateSaveRow(jsonObjData) { let sheetRowNo = jsonObjData.id; if (jsonObjData['actionkey']) { delete jsonObjData['actionkey']; } if (jsonObjData['id']) { delete jsonObjData['id']; } if(sheetRowNo) { Object.keys(jsonObjData).forEach(function(key, index) { SpreadsheetApp.getActiveSheet().getRange(sheetRowNo, index + 1).setValue(jsonObjData[key]); }); return "SUCCESS"; } return "ERROR"; } /** View a Record */ function viewRow(jsonObjData) { let findData = SpreadsheetApp.getActiveSheet().createTextFinder(jsonObjData.id).matchEntireCell(true).findNext(); if(findData) { return findData; } return 0; } /** Delete */ function deleteRow(jsonObjData) { if(jsonObjData.id) { SpreadsheetApp.getActiveSheet().deleteRow(jsonObjData.id); return "SUCCESS"; } else { return "ERROR"; } } /** Get All */ function getAllRows() { let obj = {}; let data = SpreadsheetApp.getActiveSheet().getDataRange().getValues().filter(row => row.join("")); obj = { data: data, cols: SpreadsheetApp.getActiveSheet().getDataRange().getNumColumns() } return JSON.stringify(obj); } /** Clear All */ function clearSheet() { SpreadsheetApp.getActiveSheet().clear({contentsOnly: true}); return "SUCCESS"; }
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件。
提交问题:如果有任何问题
如果有任何问题,请 联系我。