Siebel eScript varargs function always throws an exception - variadic-functions

according to Siebel documentation, eScript supports varargs.
The following sample is taken from the Siebel documentation:
function SumAll()
{
var total = 0;
for (var ssk = 0; ssk < SumAll.arguments.length; ssk++)
{
total += SumAll.arguments[ssk];
}
return total;
}
However, if I call this method like SumAll(1,2,3) I get the following exception:
TypeError: Can't convert 'Undefined' to Object. Service.SumAll line xxx
where xxx is the line number of the for statement.
Any idea, why?
Thanks!

Instead of typing "SumAll.arguments", try using just "arguments" like this:
function SumAll()
{
var total = 0;
for (var ssk = 0; ssk < arguments.length; ssk++)
{
total += arguments[ssk];
}
return total;
}

Related

NodeJS - Throw inside nested loop in Sails.js

I'm new in NodeJS and Sails.js. I get stuck when run API script like here :
try{
for(let i = 0 ; i < 10 ; i++ ) {
for(let j = 0; j < 10; j++) {
await doSomething(); // asume the API using async
throw 'a';
}
}
} catch (e) {
return e;
}
When I run the first time the script it's ok (running well), but when I run the second time, it is stuck (nothing return). When I check the console, no error appears.
It will work again if I restart the API.
What happens to my script ? can anyone explain? Why it just run 1 time (return a) but cannot started again when i call again the API without restart it first
you should throw Error not a string, so your could should look like this
throw new Error('a')
also the script won't continue after first loop cause you are throwing.
I hope its not stuck. It will return the string 'a'. You can check that by adding console.log
try {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
throw 'a';
}
}
} catch (e) {
console.log('Error: ', e);
return e;
}
This will print Error: a all the time and return 'a'
Note: As mentioned you should throw new Error instead of string 'a'.
If you want to break the loop on condition. Just set i = 11; j = 11;

Using app.ask more than once function

Within my function I would like to use app.ask('blah blah blah'); more than once. However when testing it, only the first app.ask() is spoken for which I expected it to also speak out the 2nd response too.
The function looks something like this:
for(var i = 0; i < array.length; i++) {
if (array == ##) {
app.ask(response 1)
} else {
app.ask(response 2)
}
}
That is correct - you can only use app.ask() once since that will send the response to the user. Once you send the response to the user, you must wait for the user to reply before you can send something else.
You would need to build your response in the loop and then send this to the user. Something like this
var response = '';
for(var i = 0; i < array.length; i++) {
if (array == ##) {
response += ' Message 1.';
} else {
response += ' Message 2.';
}
}
app.ask( response );

Custom module data returning successfully but not displaying

I have a custom module in file help.js, the function name there is getfriends(req,res,next,user). The user is whose friends I want to get.
for (var i = 0; i < users.length; i++) {
for (var j = 0; j < docs.length; j++) {
if (docs[j].user1 == users[i].username) {
if (docs[j].user1 != req.body.user) {
friends.push(users[i]);
}
} else if (docs[j].user2 == users[i].username) {
if (docs[j].user2 != req.body.user) {
friends.push(users[i]);
}
}
}
if (i == users.length-1) {
console.log("friends",friends); //it displays my desired result and so I think the return is successfull
return(friends);
}
}
Now where I receive the data, I do this and data is not being displayed.
console.log(Help.getfriends(req,res,next,req.session.user));
I have tried doing :-
somevar = Help.getfriends(req,res,next,req.session.user);
console.log(somevar);
The module is being called, it is displaying perfect result. Please guide me how to get the data properly from the custom module.
Also, above I have done,
var Help = require('./help');
Your function is asynchronous.
When you console.log(...) <== there is no result yet.
So i'ts expected that you console.log undefined.
More info about nodejs asyncronous nature.

Web Service in Nodejs

I want to print out all number from 1 to 1,000,000 with 1,000 numbers per line. and return the response without having a long delay.
Thanks in advance for any type of help!!!
Well, first you can create a function to print numbers from 1 to 1000.
function getThousand(index) {
var result = '';
for (var i = index; i < index + 1000; i++) {
result += i + ' ';
}
return result;
}
Then, you need a function to call this for 1 to 1000000.
function getAll() {
var result = '';
for (var i = 0; i < 1000; i++) {
result = getThousand((i * 1000) + 1) + " \n";
fs.appendFileSync('foo.txt', result);
}
}
Then call it all:
getAll();
This will save your lines into a file. At the end of getAll() you can print what you need.

Creating a attachment from Google sheet in ms Excel format via Google script

Is it possible to create an Excel format file from a Google sheet using Google script, so that it can be added as an attachment to an email?
I've got a code that takes columns with certain names (e.g. A, C, F) and turns them into a new sheet (on createCustomStatusTable() function).
https://docs.google.com/spreadsheets/d/1fZ0JMYjoIrfPIxFBVgDNU0x5X0ll201ZCU-lcaTwwcI/edit?usp=sharing
var expected = ['A','C','F'];
var newSpreadSheet = SpreadsheetApp.getActiveSheet();
var tableLastRow = newSpreadSheet.getLastRow();
var tablelastColumn = newSpreadSheet.getLastColumn();
var values = newSpreadSheet.getRange(1, 1, tableLastRow, tablelastColumn).getValues();
var rangeToCopy = [];
function in_array(value, array)
{
for(var i = 0; i < array.length; i++)
{
if(array[i] == value) return true;
}
return false;;
};
function columnsCount() {
var count = 1;
for (var i = 0; i < SpreadsheetApp.getActiveSheet().getLastColumn(); i++) {
if (in_array(values[0][i],expected))
count++;
}
return count;
};
function returnRange() {
for (var i = 1; i < SpreadsheetApp.getActiveSheet().getLastColumn()+1; i++) {
if (in_array(values[0][i-1],expected)) {
rangeToCopy.push(newSpreadSheet.getRange(1, i, newSpreadSheet.getMaxRows()));
};
};
return rangeToCopy;
};
function createCustomStatusTable() {
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Target');
for (var i = 1; i < columnsCount(); i++) {
returnRange()[i-1].copyTo(targetSheet.getRange(1,i));
};
};
Thank you in advance for any help
You can create an EXCEL type file with DriveApp:
The problem is, that the content must be a string. And I haven't tested for a way to make that work.
I know this doesn't answer your question. Hopefully someone knows for sure how to create an EXCEL file from a Google Sheet.

Resources