Google脚本 - 执行后添加成功或错误消息
首次发布海报和新手编写Google脚本。我写了一个成功工作的Ingrange脚本,但是我现在想包括某种形式的错误处理,以显示警报,具体取决于Importrange是否成功运行。因此,如果该功能成功地运行以显示成功消息,并且是否无法显示错误消息。到目前为止,我所有的尝试都没有奏效,我正在努力确定如何根据功能运行的结果合并正确的消息。以下是代码的副本。任何帮助或建议将不胜感激。
非常感谢
function runsies() {
importRange(
//Nordics
"1Xrhx03ub1uXdeFsS0mFZa2zi5uZL-qcuQDbe9oIULQU",
"DATADUMP: LE!A2:I",
"1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
"Nordics - LE!A1"
);
importRange(
//UK
"13Y0NOJie1gYWXanQhtkSnTNsx4vt_bcqfUIXj0ePnac",
"DATADUMP: LE!A2:I",
"1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
"UK - LE!A1"
);
};
function importRange(sourceID, sourceRange, destinationID, destinationRangeStart){
// Gather Source range values
const sourceSS = SpreadsheetApp.openById(sourceID);
const sourceRng = sourceSS.getRange(sourceRange);
const sourceVals = sourceRng.getValues();
// Get Destiation sheet and cell location.
const destinationSS = SpreadsheetApp.openById(destinationID);
const destStartRange = destinationSS.getRange(destinationRangeStart);
const destSheet = destStartRange.getSheet();
// Clear previous data.
destSheet.clear();
// Get the full data range to paste from start range.
const destRange = destSheet.getRange(
destStartRange.getRow(),//Start Row
destStartRange.getColumn(),//Start Column
sourceVals.length,//Row Depth
sourceVals[0].length //Column Width
);
// Paste in the values
destRange.setValues(sourceVals);
SpreadsheetApp.flush();
};
First time poster and newbie to writing Google Scripts. I have written an importrange script which is working successfully, but I would now like to include some form of error handling to display an alert depending on whether the importrange has run successfully or not. So, if it the function runs successfully to display a success message and if it fails to display an error message. All my attempts so far haven't worked and I'm struggling to identify how I incorporate the correct message depending on the outcome of the function running. Below is a copy of code. Any help or advice would be greatly appreciated.
Many thanks
function runsies() {
importRange(
//Nordics
"1Xrhx03ub1uXdeFsS0mFZa2zi5uZL-qcuQDbe9oIULQU",
"DATADUMP: LE!A2:I",
"1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
"Nordics - LE!A1"
);
importRange(
//UK
"13Y0NOJie1gYWXanQhtkSnTNsx4vt_bcqfUIXj0ePnac",
"DATADUMP: LE!A2:I",
"1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
"UK - LE!A1"
);
};
function importRange(sourceID, sourceRange, destinationID, destinationRangeStart){
// Gather Source range values
const sourceSS = SpreadsheetApp.openById(sourceID);
const sourceRng = sourceSS.getRange(sourceRange);
const sourceVals = sourceRng.getValues();
// Get Destiation sheet and cell location.
const destinationSS = SpreadsheetApp.openById(destinationID);
const destStartRange = destinationSS.getRange(destinationRangeStart);
const destSheet = destStartRange.getSheet();
// Clear previous data.
destSheet.clear();
// Get the full data range to paste from start range.
const destRange = destSheet.getRange(
destStartRange.getRow(),//Start Row
destStartRange.getColumn(),//Start Column
sourceVals.length,//Row Depth
sourceVals[0].length //Column Width
);
// Paste in the values
destRange.setValues(sourceVals);
SpreadsheetApp.flush();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述
您可以使用try {...} catch(err){...}块来捕获代码中可能出现的任何错误或出口期间。并且您可以使用Everdersheetapp.getui()。alert()显示一个简单的警报对话框。
code.gs
参考
Description
You can use a try {...} catch(err) {...} block to catch any error that may occur in your code or during exection. And you can use SpreadsheetApp.getUi().alert() to display a simple alert dialog box.
Code.gs
Reference