我有一个谷歌应用程序脚本工作好几个月,它突然停止工作。我想知道谷歌是否弃用了我的部分代码或其他东西。
这是指向文件的链接:Click here to view Google Spreadsheet File
当尝试从HTML文件调用Google脚本函数时,Google脚本代码显然失败了。这是失败的代码行。
google.script.run.importCSVData(id);
这些行失败并捕获以下错误:
TypeError:无法读取未定义的属性“run”
就像我说的那样,我让这段代码运行了好几个月,它突然停止了工作。
(仅供参考...此代码的目的是将CSV文件上传到“Archivo Plano ADN”选项卡。要运行此代码,必须选择“Importar Archivo CSV” - >“Importar Archivo”。 ..“从上方菜单中。当提示选择文件时,选择任何.csv文件.csv文件必须用;分隔。)
我的Google电子表格文件有三个代码文件(“Code.gs”,“CodeImport.gs”和“Picker.html”)
让我给你一个代码:
Code.gs:
function onOpen() {
var me = Session.getEffectiveUser();
if (me.getEmail() == "alejandro.sardi@crusardi.net") {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Protected Ranges')
.addItem('Remove Protection', 'menuItem1')
.addItem('Copy Protected Ranges To Another Sheet', 'menuItem2')
.addItem('Copy SM Protected Ranges to all SA sheets', 'menuItem3')
.addToUi();
ui.createMenu('Importar Archivo CSV')
.addItem('Importar Archivo...', 'showPicker')
.addToUi();
ui.cre
} else {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Importar Archivo CSV')
.addItem('Importar Archivo...', 'showPicker')
.addToUi();
}
}
CodeImport.gs:
function importCSVData(id) {
try {
var file = DriveApp.getFileById(id);
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString("ISO-8859-1"),";");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Archivo Plano ADN");
sheet.getRange("A:U").clear();
sheet.getRange("G:G").setNumberFormat('@STRING@');
Logger.log("yes");
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
file.setTrashed(true);
SpreadsheetApp.getUi().alert("Archivo Importado con éxito");
} catch (e) {
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Error report jaja - sonríele a la vida!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
}
}
function showPicker() {
var html = HtmlService.createHtmlOutputFromFile('Picker.html')
.setWidth(600)
.setHeight(425)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(html, 'Select File');
}
function getOAuthToken() {
DriveApp.getRootFolder();
return ScriptApp.getOAuthToken();
}
function finishedImport() {
SpreadsheetApp.getUi().alert("Archivo importado exitosamente");
}
function senderror(e) {
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Error report jaja - sonríele a la vida!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
}
最后,Picker.html文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<script type="text/javascript">
var DIALOG_DIMENSIONS = {
width: 600,
height: 425
};
var pickerApiLoaded = false;
function onApiLoad() {
gapi.load('picker', {
'callback': function() {
pickerApiLoaded = true;
}
});
google.script.run.withSuccessHandler(createPicker)
.withFailureHandler(showError).getOAuthToken();
}
function createPicker(token) {
if (pickerApiLoaded && token) {
//var docsView = new google.picker.DocsView()
//.setIncludeFolders(true)
//.setMimeTypes('application/vnd.google-apps.folder')
//.setSelectFolderEnabled(true);
var uploadDocsView = new google.picker.DocsUploadView()
.setIncludeFolders(true)
//.setMimeTypes('application/vnd.google-apps.folder')
//.setSelectFolderEnabled(true);
var picker = new google.picker.PickerBuilder()
//.addView(docsView)
.addView(uploadDocsView)
//.setAppId("AIzaSyCZDa4JKKIOv2AF3QyrG8DnVOXmz27054o")
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.hideTitleBar()
.setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2)
.setOAuthToken(token)
.setCallback(pickerCallback)
.setOrigin('https://docs.google.com')
.build();
picker.setVisible(true);
} else {
showError('Unable to load the file picker.');
}
}
/**
* A callback function that extracts the chosen document's metadata from the
* response object. For details on the response object, see
* https://developers.google.com/picker/docs/result
*
* @param {object} data The response object.
*/
function pickerCallback(data) {
try {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var id = doc[google.picker.Document.ID];
// Show the ID of the Google Drive folder
//document.getElementById('result').innerHTML = id;
document.getElementById('result').innerHTML = "Importando..."
google.script.run.importCSVData(id);
//google.script.run.deleteImportedFile(id);
google.script.host.close();
} else if (action == google.picker.Action.CANCEL) {
//document.getElementById('result').innerHTML = "Cerrando1..."
google.script.host.close();
//document.getElementById('result').innerHTML = "Cerrando2..."
}
} catch (e) {
document.getElementById('result').innerHTML = e;
google.script.run.senderror(e);
}
}
function showError(message) {
document.getElementById('result').innerHTML = 'Error: ' + message;
}
</script>
</head>
<body>
<div>
<p id='result'></p>
</div>
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>
答案 0 :(得分:8)
您需要使用Google API Loader库加载Google文件选择器。
替换:
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
使用:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>google.load("picker", "1", {callback:function(){pickerApiLoaded =!0}});</script>
请务必在进行此更改后发布新版本的网络应用。
更新:来自Google Apps脚本小组的Erik说&#34;问题的原因是当Picker API加载到google.picker时,它当前正在覆盖google.script,因此google.script.run()调用开始失败。&#34;
他们发布了alternate solution - 在加载Picker API时手动保留并恢复google.script:
window.script = google.script;
gapi.load('picker', '1', {callback: function() {
google.script = window.script;
// ...
}});