在这个例子中最好抛出异常的地方?(Where best to throw exception in this example?)
在我正在编写的库中,我有一些方法只能在很少的时间内执行。 当我对车辆进行建模时,其中一种(例外)方法是破解引擎,这种情况非常罕见。 我应该在这种方法中抛出异常吗? 这将阻止车辆使用。
我阅读了.NET设计框架指南一书,它指出只有在方法无法完成执行时才会发生异常。 实际的引擎破解方法将始终完成,但是如果我的一个方法,例如启动引擎(此方法将存储引擎启动的数量作为累积引擎压力的方法)被调用并导致调用破解引擎,在哪里(如果有的话)我应该抛出异常?
In a library I am writing, I have some methods which would only be executed very rarely and on the fly. As I am modelling a vehicle, one of the (Exceptional) methods is a cracked engine, which would be very rare. Should I throw an exception in this method? This will stop a vehicle from being usable.
I read the .NET Design Framework Guidelines book and it states that an exception should only occur when a method cannot complete execution. The actual engine cracked method will always complete, but if one of my methods, such as start engine (this method will store the amount of engine starts as a means of accruing stress to the engine), is called and results in a call to cracked engine, where (if anywhere) should I throw an exception?
原文:https://stackoverflow.com/questions/4491623
最满意答案
这里是你的数组和循环的代码:
function Transfer() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s1 = ss.getSheetByName('Monday-Saturday'); var s2 = ss.getSheetByName('2ndTrailer'); var data1 = s1.getRange(5, 3, 18, 2).getValues(); var data2 = s2.getRange(3, 2, 18, 2).getValues(); var newData = []; for(var i = 0 ; i < data1.length; i++){ var newValueC = data1[i][0] + data2[i][1]; var newValueD = data1[i][1] + data2[i][1]; newData.push([newValueC, newValueD]); } s1.getRange(5, 3, 18, 2).setValues(newData); s2.getRange('A2').clearContent(); s1.getRange('A23').setValue('YES'); }
有点难看,但应该做的伎俩。
编辑:做了一些更改,因为range.setValue()方法导致问题
Here is your code with arrays and loop :
function Transfer() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s1 = ss.getSheetByName('Monday-Saturday'); var s2 = ss.getSheetByName('2ndTrailer'); var data1 = s1.getRange(5, 3, 18, 2).getValues(); var data2 = s2.getRange(3, 2, 18, 2).getValues(); var newData = []; for(var i = 0 ; i < data1.length; i++){ var newValueC = data1[i][0] + data2[i][1]; var newValueD = data1[i][1] + data2[i][1]; newData.push([newValueC, newValueD]); } s1.getRange(5, 3, 18, 2).setValues(newData); s2.getRange('A2').clearContent(); s1.getRange('A23').setValue('YES'); }
A bit ugly, but should do the trick.
Edit: Made a few changes, since the range.setValue() method causing problems
相关问答
更多-
这里是你的数组和循环的代码: function Transfer() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s1 = ss.getSheetByName('Monday-Saturday'); var s2 = ss.getSheetByName('2ndTrailer'); var data1 = s1.getRange(5, 3, 18, 2).getValues(); var data2 = s2.getRa ...
-
如果你可以接受格式上的小改动,那么你可以利用其中一个'值',例如: replaceInSheet(sheet,'values','1. Values'); If a small change in formatting is acceptable to you then you can capitalize one of the 'values', ex: replaceInSheet(sheet,'values','1. Values');
-
数组没有getBlob方法。 您必须使用Utilities.newBlob()从字符串中获取blob。 您可以在此处找到相同的文档 var rowsCSV = rows.join("\n"); var blob = Utilities.newBlob(rowsCSV, "text/csv") Logger.log(blob.getDataAsString()) var data = blob.setContentType('application/octet-stream'); 同样,你可以做到这 ...
-
这个修改怎么样? 修改要点: 在你的脚本中, "[\"" + arr[i] + "\"]"成为字符串。 你可以使用[arr[i]] 。 你可以把array = toMatrix(array); 到for循环之外。 修改脚本: // function pastValues should past 6 columns each comprised of a,b,c,d,e,f. Each letter is in its own cell. function pasteValues() { var shee ...
-
在构建MultipleChoise对象时存在一些小问题: //Form - globally accessible var f = FormApp.openById('someID'); //Date Iterator var curr_date = 0; //Time Iterator var curr_time = 0; //Array of Times var Tchoices = []; //Setting Time choices per date whi ...
-
getSheetByName(名称) var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses"); https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheetByName(String) getSheetByName(name) var sheet = SpreadsheetApp.getActiveSp ...
-
你找到的东西不能干活。 当外部代码使用电子表格或驱动api更改内容时,当前不会调用这些触发器。 如果你需要它实时工作,它是不可能的。 否则,如果您可以延迟1分钟,则可以使用重复的1分钟触发器来检查电子表格上的最后修改日期是否已更改(通过与先前存储在脚本属性中进行比较)。 如果它改变了,再次处理整个表格。 what you found cannot be worked arround. currently those triggers do not get called when external code ...
-
影响程度将取决于您正在进行多少阅读和写作。 如果目前使用了大量增量数据传输,那么改变您的方法可以带来巨大的收益。 调用电子表格API通常需要0.01秒或更长时间才能完成。 调用导入/导出数据或调用子函数将需要更长的时间(例如, getDataRange , getValue(s) , setValue(s) )。 Apps脚本非常智能,可以优化一些连续的调用,但如果您正在交替读写,那么Google的任何内容都无法为您提供帮助。 您可以在“ View -> Execution Transcript菜单上查看此 ...
-
在单页而不是整个电子表格上运行Google脚本(Run Google script on single sheet instead of entire spreadsheet)[2022-10-17]
如果onFormSubmit函数位于绑定到电子表格的脚本上,则事件对象包含范围对象。 您可以使用getSheet获取工作表,然后使用getName获取工作表名称。 例: function onFormSubmit(e){ // Initialize var rng = e.range; var sheet = rng.getSheet(); var name = sheet.getName(); // If the response was not submitted to the r ... -
Google表格脚本错误 - 无法将数组转换为对象[] [](Google Sheets Script Error - Cannot convert Array to Object[][])[2023-08-27]
问题是data2set[0]永远不会被初始化(它总是NULL)。 解决该问题的最简单方法是在输出到电子表格之前删除data2set [0]: data2set.shift(); // remove 1st item setRange.setValues(data2set); The problem is that data2set[0] is never initialized (it is always NULL). The easiest way to fix the problem is t ...