How to copy image attribute from one data source to another in wakanda with script? - wakanda

I use this code to copy data from one Data Class to another :
sources.dataClass1.attribute1 = sources.dataClass2.attribute1;
it work for all attribute type except image, how to copy image attribute from one data source to another ?
Regards,
Sam

It works for me using this script :
var ds1 = ds.dataClass1.first();
var ds2 = new ds.dataClass2();
ds2.image = ds1.image;
ds2.save();`

Related

Google sheets script Exceeded maximum execution time

I wrote a script to import stock data from a csv file stored in Google Drive to an existing google sheet.
In one function I'm doing this for multiple csv files. Unfortunately I get "Exceeded maximum execution time" sometimes, but not all the time.
Do you have an idea how I can boost performance on this:
//++++++++++++++ SPY +++++++++++++++++++
var file = DriveApp.getFilesByName("SPY.csv").next();
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
//Create new temporary sheet
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.getSheetByName("SPY-Import");
if (yourNewSheet != null) {
activeSpreadsheet.deleteSheet(yourNewSheet);
}
yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName("SPY-Import");
//Import
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
//Copy from temporary sheet to destination
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A:B').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SPY'), true);
spreadsheet.getRange('A2').activate();
spreadsheet.getRange('\'SPY-Import\'!A:B').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
//Delete temporary sheet
// Get Spreadsheet Object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Get target sheet object
var sheet = spreadsheet.getSheetByName("SPY-Import");
// Delete
spreadsheet.deleteSheet(sheet);
Thanks in advance!
I believe your situation and goal as follows.
You have several CSV files like SPY.csv.
Your Spreadsheet has the several sheet corresponding to each CSV file like SPY.
You want to put the values from the CSV data to the Spreadsheet.
You want to put the values of the column "A" and "B" of the CSV data.
In your current situation, you copied the script in your question several times and run them by changing the csv filename and sheet name.
You want to reduce the process cost of your script. I understood your goal like this.
Modification points:
SpreadsheetApp.getActiveSpreadsheet() is used several times. And, activate() is used several times.
I think that in your case, SpreadsheetApp.getActiveSpreadsheet() can be declared one time, and activate() is not required to be used.
In order to copy the CSV data to Spreadsheet, the CSV data is put to a temporal sheet and the required values are copied to the destination sheet.
In this case, I think that the CSV data is directly put to the destination sheet by processing on the array.
I think that above points lead to the reduction of process cost. When above points are reflected to your script, it becomes as follows.
Modified script:
Please copy and paste the following script and prepare the variable of obj. When you run the script, the CSV data is retrieved and processed, and then, the values are put to the Spreadsheet.
function myFunction() {
var obj = [
{filename: "SPY.csv", sheetname: "SPY"},
{filename: "###.csv", sheetname: "###"},
,
,
,
];
var ss = SpreadsheetApp.getActiveSpreadsheet();
obj.forEach(({filename, sheetname}) => {
var file = DriveApp.getFilesByName(filename);
if (file.hasNext()) {
var sheet = ss.getSheetByName(sheetname);
if (sheet) {
// sheet.clearContents(); // Is this requierd in your situation?
var csv = DriveApp.getFileById(file.next().getId()).getBlob().getDataAsString();
var values = Utilities.parseCsv(csv).map(([a, b]) => [a, b]);
sheet.getRange(2, 1, values.length, 2).setValues(values);
}
}
});
}
Note:
Please use this script with enabling V8
I'm not sure about your CSV data. So when Utilities.parseCsv(csv) cannot be used, please use the delimiter.
In this modification, Spreadsheet service is used. If above modified script occurs the same error of Exceeded maximum execution time, please tell me. At that time, I would like to propose the sample script using Sheets API.
References:
Spreadsheet Service
parseCsv(csv)

How to specify the file name when saving the model using h2o package from R

I am trying to save the model build using the function: h2o.saveModel(), based on function description on page 159 of the H2O user manual for R, the arguments only consider path. I looked at other similar function such as: h2o.saveModelDetails() but it uses the same argument. Please advise if there any another way to specify the name of the model.
The name of the model file will be determined by the ID of the model. So if you specify model_id when training your model, then you can customize it. Right now there is no way to change the ID of the model after it's been trained.
The file can be renamed once saved:
h2o.saveModel(object = fit, path = path.value, force = TRUE) # force overwriting
name <- file.path(path.value, fileName) # destination file name at the same folder location
file.rename(file.path(path.value, fit#model_id), name)
I think a better work around would be to generate a unique folder each time saving the model. When loading model there will always be only one model file under the path.
saved_model = os.path.join('UNIQUE_MODEL_PATH', os.listdir('UNIQUE_MODEL_PATH')[0])
loaded_model = h2o.load_model(saved_model)
in python :
model_path = h2o.save_model(model=model, path="mymodel1", force=True)
path = os.path.dirname(os.path.abspath(model_path))
os.rename(model_path, os.path.join(path,f'h2o_new_name'))
Here a possible way to do it:
output_dir <-getwd()
DRF_MO <- h2o.saveModel(object=aml, path=output_dir, force=TRUE)
DRF_MO <- file.path(output_dir, aml#algorithm)
file.rename(file.path(output_dir, aml#model_id), DRF_MO)

Wakanda Datastore - Find and Replace?

I've got a lot of values in a legacy Wakanda datastore which I need to update to some new values. Is there a curl-like command in the wakanda data browser page that can be used to do a mass find-and-replace in a table?
If your dataclass is called MyDataClass and the attribute you want to update is myAttribute you can use the following server-side script :
var newValue = "new value";
ds.MyDataClass.all().forEach(function(entity){
entity.myAttribute = newValue;
entity.save();
});
You can also use a transaction if you want to commit or rollback the whole operation
I don't think there is a way to do a mass of find/replace in the dataBrowser,
But I suggest you to use a query in the server side that search the records with the value you need to replace, and then a loop on this collection to set the new values
As mentioned in other answers, you are likely best to loop over a collection. There is no concept of a mass replace in Wakanda like you see in many other databases.
var myCollection = ds.DataClassName.query("attributeName == :1", "valueToFind");
myCollection.forEach(function(e){
e.attributeName = "newValue";
e.save();
});
So a fake "person" data type might look like this:
var blankFirsts = ds.Person.query("firstname == :1", "");
blankFirsts.forEach(function(person){
person.firstname = "no name";
person.save();
});

Updating an Entity Without Saving the Data back to the Database

I have created a new query like the following
var pressData = from press in dataContext.Releases
select new
{
Heading = press.Heading,
Description = press.Desc,
DatePublished = press.PublishDate.ToString(),
Body = press.BodyContent,
ID=press.ReleaseID,
CreatedBy=press.CreatedBy
};
Later in the code I want to update the entity from a session variable, but not save any data back to the database. Here is the code I am trying to accomplish this with....
var edit = pressData.Where(a => a.Heading == sectionPreview.HeadingContent && a.ID == sectionPreview.tionID).FirstOrDefault();
if (edit != null)
{
//WONT LET ME UPDATE THE Body VALUE
edit.Body = sectionPreview.SectionContent;
}
The code aboves purpose is to look at pressData and replace the body content with the new body from a session variable(not shown here), but NOT save it to the db. I want pressData to be filtered and updated only in the entity. So when I bind it to the control in this case it binds the data stored in my session.
this.rptSections.DataSource = pressData;
this.rptSections.DataBind();
I am getting a complier error stating
Property or indexer 'AnonymousType#1.Body' cannot be assigned to -- it is read only.
I checked the entity model and nothing is read only not any fields not anything. I must be missing something?
Anonymous Types encapsulate a read only property collection - for more information, read here. The compiler rewrites anonymous types as a constructor injections, ie:
select new
{
Heading = press.Heading,
Description = press.Desc,
DatePublished = press.PublishDate.ToString(),
Body = press.BodyContent,
ID=press.ReleaseID,
CreatedBy=press.CreatedBy
};
Is really rewritten as:
new Anonymous`1(press.Heading, press.Desc, press.PublishDate.ToString(), press.BodyContent, press.ReleaseID, press.CreatedBy)
And the properties are read only (public get, private / protected set, to use an easy comparison). If you want to solve your issue, instead of taking the data and making an anonymous object, create a real type and set properties on it.

How to manually load an entity as well as a related entity

I have two entity objects one that holds billing address information(TBLADDRESS) and one that holds my account addresses(TBLMYACCOUNTADDRESS).
At a point in my project i need to load the TBLADDRESS object with the corresponding values from TBLMYACCOUNTADDRESS. Both of which have a relation to LKSTATE.
My problem is i cannot populate this related entity manually. Maybe it's not possible?
Here is my script so far, hopefully it will help you better understand exactly what i am trying to accomplish:
TBLADDRESS tblBilling = new TBLADDRESS();
TBLMYACCOUNTADDRESS myAccountDefaultAddress = new TBLMYACCOUNTADDRESS();
myAccountDefaultAddress = myAccountAddress.FindAll(delegate(TBLMYACCOUNTADDRESS i) { return i.IS_DEFAULT == true; }).ToList().SingleOrDefault();
tblBilling = new TBLADDRESS();
tblBilling.FIRST_NAME = myAccountDefaultAddress.FIRST_NAME;
tblBilling.LAST_NAME = myAccountDefaultAddress.LAST_NAME;
tblBilling.COMPANY = myAccountDefaultAddress.COMPANY;
tblBilling.ADDRESS_1 = myAccountDefaultAddress.ADDRESS_1;
tblBilling.ADDRESS_2 = myAccountDefaultAddress.ADDRESS_2;
tblBilling.CITY = myAccountDefaultAddress.CITY;
tblBilling.LKSTATE.STATE_ID = myAccountDefaultAddress.LKSTATE.STATE_ID;
tblBilling.POSTAL_CODE = myAccountDefaultAddress.POSTAL_CODE;
tblBilling.PHONE = myAccountDefaultAddress.PHONE;
Question is how would i go about populating the tblBilling.LKSTATE.STATE_ID manually. Currently i get an object reference not set to an instance of an object error message, but something tells me there's more to it than that.
Thanks in Advance,
Billy
Figured it out.
All i need was the instance of the object created before assigning to it.
tblBilling.LKSTATE = new LKSTATE();
Initially i wasn't sure just how to create the instance. Pretty straightforward.

Resources