因此,電子表格包含多個功能,這些功能在編輯者單擊按鈕時運行。某些功能會從其他檔案中獲取資料,而編輯者無權訪問這些資料。因此,資料被帶入保護范圍。編輯器將他們的輸入添加到不受保護的范圍內,當他們單擊保存時,結果將保存回其他檔案中。
如何訪問另一個檔案的示例:檔案:globals.gs
const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
const config = {
get ssBDCadProd() {
delete this.ssBDCadProd;
return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
}
}
const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
這是一個使用上述檔案中的資料的函式:
//It generates the next item code, based on the last record in the file above
function gerarRef() {
try {
const response = Browser.msgBox('Would you like to confirm it?', Browser.Buttons.YES_NO);
if (response == 'no') {
return;
} else {
let ref = 0;
const refExistentes = sheetBDCadProd.getRange(2, 1, sheetBDCadProd.getLastRow(), 1).getValues();
let ultRef = Math.max(...refExistentes);
Logger.log('Ult.: ' ultRef)
if (ultRef == 0 || ultRef == '') {
ref = 10000;
} else {
ref = ultRef 1;
}
const refRng = sheetCadProd.getRange('B5').setValue(ref);
refRng.offset(0, 2).activate();
}
} catch (err) {
Browser.msgBox('The following error has occurred: ' err);
}
}
我了解該Web App方法需要doGet()向其中添加一個功能,并且要使用Executed by: Meand進行部署Accessed by: Anyone。但是,我不知道如何將它與系結到電子表格的現有功能聯系在一起。
感謝您的關注/幫助!
uj5u.com熱心網友回復:
在您的情況下,如何進行以下修改?
1.修改腳本:
請將以下腳本復制并粘貼到腳本編輯器并保存腳本。
在這個腳本中,sheetCadProd沒有宣告,因為我在你的顯示腳本中看不到它。請注意這一點。
function doGet() {
const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
const config = {
get ssBDCadProd() {
delete this.ssBDCadProd;
return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
}
}
let ref = 0;
const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
const refExistentes = sheetBDCadProd.getRange(2, 1, sheetBDCadProd.getLastRow(), 1).getValues();
let ultRef = Math.max(...refExistentes);
Logger.log('Ult.: ' ultRef);
if (ultRef == 0 || ultRef == '') {
ref = 10000;
} else {
ref = ultRef 1;
}
const refRng = sheetCadProd.getRange('B5').setValue(ref);
return ContentService.createTextOutput(JSON.stringify({ range: refRng.offset(0, 2).getA1Notation() }));
}
//It generates the next item code, based on the last record in the file above
function gerarRef() {
try {
const response = Browser.msgBox('Would you like to confirm it?', Browser.Buttons.YES_NO);
if (response == 'no') {
return;
} else {
const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec");
console.log(res.getContentText());
const { range } = JSON.parse(res.getContentText());
sheetCadProd.getRange(range).activate();
}
} catch (err) {
Browser.msgBox('The following error has occurred: ' err);
}
}
2. 部署 Web 應用程式。
詳細資訊可以看官方檔案。
- 在腳本編輯器上,在腳本編輯器的右上角,請點擊“點擊部署”->“新建部署”。
- 請點擊“選擇型別”->“Web App”。
- 請在“部署配置”下的欄位中輸入有關 Web App 的資訊。
- 請為“執行為”選擇“我” 。
- 這是此解決方法的重要性。
- 請為“誰有權訪問”選擇“任何人” 。
- 在你的情況下,我認為這個設定可能是合適的。
- 請點擊“部署”按鈕。
- 復制 Web 應用的 URL。就像
https://script.google.com/macros/s/###/exec。- When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
- You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
And also, please set your Web Apps URL to const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec"); of the above script. And, please reflect the latest script to the Web Apps again. By this, your script works. Please be careful this.
3. Testing.
Before you use this script, please check sheetCadProd. In my modified script, sheetCadProd is not declared, because I cannot see it in your showing script. Please be careful this.
In the above script, please run gerarRef(). By this, the script of Web Apps is run by the owner of Web Apps.
Note:
- When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
- You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
References:
- Web Apps
- Taking advantage of Web Apps with Google Apps Script
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449472.html
標籤:谷歌应用脚本 谷歌表格 webapp2 角色基础授权
