Google脚本 - 执行后添加成功或错误消息

发布于 2025-02-12 05:01:07 字数 1535 浏览 1 评论 0原文

首次发布海报和新手编写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-02-19 05:01:07

描述

您可以使用try {...} catch(err){...}块来捕获代码中可能出现的任何错误或出口期间。并且您可以使用Everdersheetapp.getui()。alert()显示一个简单的警报对话框。

code.gs

function importRange(sourceID, sourceRange, destinationID, destinationRangeStart){
  try {
    // Gather Source range values
    const sourceSS = SpreadsheetApp.openById(sourceID);
    //
    // The rest of your code here
    //
    SpreadsheetApp.flush();
    SpreadsheetApp.getUi().alert("Success");
  }
  catch(err) {
    SpreadsheetApp.getUi().alert("Error: "+err);
  }
};

参考

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

function importRange(sourceID, sourceRange, destinationID, destinationRangeStart){
  try {
    // Gather Source range values
    const sourceSS = SpreadsheetApp.openById(sourceID);
    //
    // The rest of your code here
    //
    SpreadsheetApp.flush();
    SpreadsheetApp.getUi().alert("Success");
  }
  catch(err) {
    SpreadsheetApp.getUi().alert("Error: "+err);
  }
};

Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文