A vendor is providing us a csv file nightly. We need to take that csv, pull out some of the columns, then import them into our in-house application. The csv, as we receive it, looks a bit like this:
StudentId, GradYear, 2014 Thing1, 2014 Thing2, 2015 Thing1, 2015 Thing2
and so on, adding columns seemingly to perpetuity.
When I first saw this csv, my first inclination was to transpose it to 2 columns: thus:
StudentId, 123456
GradYear, 2016
2014 Thing1, overdue
2015 Thing1, completed
My inclination is use csvHelper to read this file in, and transpose it by constructing an ILookup. So I would have a Lookup that would look like:
var theLookup = ILookup<string, KeyValuePair<string, string>>
The key being the StudentId, and the KeyValuePair being the column header : cell value.
I can almost work it out using a Dictionary, with a counter for the dictionary key, but I can't make the leap to Lookup with the StudentId as the key.
Here is how I'm constructing the Dictionary:
Dictionary<int, List<KeyValuePair<string, string>>> vendorDictionary = new Dictionary<int, List<KeyValuePair<string, string>>>();
using (var fileReader = File.OpenText(sourcePath))
using (var reader = new CsvHelper.CsvReader(fileReader))
{
while (reader.Read())
{
var dynamicVendor = reader.GetRecord<dynamic>() as IDictionary<string, object>;
foreach (var rec in dynamicVendor)
{
var recordDictionary = dynamicVendor.ToDictionary(k => k.Key, k => k.Value.ToString());
vendorDictionary.Add(counter, recordDictionary.ToList());
++counter;
}
}
I can view the records with the following code. Do I need to do a version of this foreach to make into a Lookup? Or, should I just use a Dictionary with some "if it's 'StudentId' then make it the key?
foreach (var rec in vendorDictionary)
{
Console.WriteLine("Vendor Dictionary record # " + rec.Key + " values are:");
foreach (var value in rec.Value)
{
Console.WriteLine("Key: '" + value.Key + "' Value: '" + value.Value + "'");
}
}
Mostly I want to know if I'm even on the right track here.
When I have whatever object of vendor data I end up with, I will need to loop through it, compare student with our student record and make decisions about updating records in our app.
Related
In a table (in excel) in a column I have some number(A).
I want the flow to take that number (A) and to create number of rows equels to Number (A)
For example if number(A) is 4, then in another table to be added 4 rows
I've made an assumption on the source and destination tables. This concept can be adjusted and applied to suit your own scenario.
I'd be using Office Scripts to do this. If you've never used it then feel free to consult the Microsoft documentation to get you going ...
https://learn.microsoft.com/en-us/office/dev/scripts/tutorials/excel-tutorial
This is the script you need to create (change the name of your tables accordingly) ...
function main(workbook: ExcelScript.Workbook)
{
var addRowsTable = workbook.getTable('TableRowsToAdd');
var addRowsToTable = workbook.getTable('TableAddRowsToTable');
var addRowsTableDataRange = addRowsTable.getRangeBetweenHeaderAndTotal();
var addRowsTableDataRangeValues = addRowsTableDataRange.getValues();
// Sum the values so we can determine how many more rows need to be added
// to the destination table.
var sumOfAllRowsToBeInExistence = 0;
for (var i = 0; i < addRowsTableDataRangeValues.length; i++) {
if (!isNaN(addRowsTableDataRangeValues[i][0])) {
sumOfAllRowsToBeInExistence += Number(addRowsTableDataRangeValues[i][0]);
}
}
var currentRowCount = addRowsToTable.getRangeBetweenHeaderAndTotal().getRowCount();
var rowsToAdd = sumOfAllRowsToBeInExistence - currentRowCount;
console.log(`Current row count = ${currentRowCount}`);
console.log(`Rows to add = ${rowsToAdd}`);
if (rowsToAdd > 0) {
/*
The approach below is contentious given the performance impact but this approach ...
for (var i = 1; i <= rowsToAdd; i++) {
... didn't always yield the correct result. May be a bug but needs investigation.
Ultimately, there are a few ways to achieve the same result, like using the resize method.
This was the easiest option for a StackOverflow answer.
*/
while (addRowsToTable.getRangeBetweenHeaderAndTotal().getRowCount() <
sumOfAllRowsToBeInExistence) {
addRowsToTable.addRows();
}
}
}
You can then call that from PowerAutomate using the Run script action under Excel Online (Business) ...
You can use that approach or all of the actions that are available in PowerAutomate which will achieve the same sort of thing.
IMO, Using Office Scripts is much easier. Creating a large flow can be a real pain in the backside to deal with given there'll be a whole heap of actions that you'll need to throw in to reach the same outcome.
I would pass the number of rows to add in an office scripts script as a parameter. Once you have the value, create a JSON string of a 2d array. You want to create a loop using the number of rows to add. In the loop you continue to concatenate the 2d array. Once you've exited the loop, parse the JSON string and add the 2d array to the table. You can see how you code might look below:
function main(workbook: ExcelScript.Workbook, rowsToAdd: number)
{
//set table name
let tbl = workbook.getTable("table2")
//initialize json string with open bracket
let jsonArrString = "["
//set the temp json string with a 2d array
let tempJsonArr = '["",""],'
//concatenate json string equal to the number of rows to add
for (let i = 0; i < rowsToAdd; i++){
jsonArrString += tempJsonArr
}
//remove extra comma from JSON string
jsonArrString = jsonArrString.slice(0, jsonArrString.length-1)
//add closing bracket to JSON string
jsonArrString += "]"
//parse json string into array
let jsonArr: string[][] = JSON.parse(jsonArrString)
//add array to table to add the number of rows
tbl.addRows(null,jsonArr)
}
I'm designing a script that takes an object (jsonData[data]) and inputs its values into a different sheet based on which product it is.
Currently the script inputs all the data into a new row each time the form reaches a new stage, however the form goes through 4 stages of approval and so I'm finding each submission being entered into 4 different rows. Each submission has an "Id" value within the object which remains the same (but each submission could also be on any row in the sheet as it's used a lot).
I'm checking whether the ID exists in the sheet and using iteration to find the row number:
function updatePlatformBulkInfo(jsonData) {
var sheetUrl = "https://docs.google.com/spreadsheets/d/13U9r9Lu2Fq1WTT8pQ128heCm6_gMmH1R4O6u8e7kvBo/edit#gid=0";
var sheetName = "PlatformBulkSetup";
var doc = SpreadsheetApp.openByUrl(sheetUrl);
var sheet = doc.getSheetByName(sheetName);
var rowList = [];
var formId = jsonData["Id"];
var allSheetData = sheet.getDataRange().getValues();
setLog("AllSheetData = " + allSheetData[1][11]) //Logs to ensure data is collected correctly
var rowEdited = false;
var rowNumber = 0;
//Check whether ID exists in the sheet
for (var i = 0; i < allSheetData.length; i++) {
if(allSheetData[i][11] == formId) {
rowEdited = true;
} else {
rowNumber += 1;
}
}
My issue is with the next part:
//Append row if ID isn't duplicate or update row if duplicate found
if (rowEdited == false) {
for (var data in jsonData) {
rowList.push(jsonData[data])
}
setLog("***Row List = " + rowList + " ***");
setLog("***Current Row Number = " + rowNumber + " ***");
sheet.appendRow(rowList);
} else if(rowEdited == true){
var newRowValue = jsonData[data];
sheet.getRange(rowNumber, 1).setValues(newRowValue);
}
Everything works fine if the duplicate isn't found (the objects values are appended to the sheet). But if a duplicate is found I'm getting the error:
Cannot find method setValues(string)
This looks to me like i'm passing a string instead of an object, but as far as I'm aware I've already converted the JSON string into an object:
var jsonString = e.postData.getDataAsString();
var jsonData = JSON.parse(jsonString);
How can I modify my script to write the updated data to the matched row?
It's unclear based on your code whether or not you will actually write to the correct cell in the case of a duplicate. As presented, it looks as though you loop over the sheet data, incrementing a row number if the duplicate is not found. Then, after completing the loop, you write to the sheet, in the row described by rowNumber, even though your code as written changes rowNumber after finding a duplicate.
To address this, your loop needs to exit upon finding a duplicate:
var duplicateRow = null, checkedCol = /* your column to check */;
for(var r = 0, rows = allSheetData.length; r < rows; ++r) {
if(allSheetData[r][checkedCol] === formId) {
// Convert from 0-base Javascript index to 1-base Range index.
duplicateRow = ++r;
// Stop iterating through allSheetData, since we found the row.
break;
}
}
In both cases (append vs modify), you seem to want the same output. Rather than write the code to build the output twice, do it outside the loop. Note that the order of enumeration specified by the for ... in ... pattern is not dependable, so if you need the elements to appear in a certain order in the output, you should explicitly place them in their desired order.
If a duplicate ID situation is supposed to write different data in different cells, then the following two snippets will need to be adapted to suit. The general idea and instructions still apply.
var dataToWrite = [];
/* add items to `dataToWrite`, making an Object[] */
Then, to determine whether to append or modify, test if duplicateRow is null:
if(dataToWrite.length) {
if(duplicateRow === null) {
sheet.appendRow(dataToWrite);
} else {
// Overwriting a row. Select as many columns as we have data to write.
var toEdit = sheet.getRange(duplicateRow, 1, 1, dataToWrite.length);
// Because setValues requires an Object[][], wrap `dataToWrite` in an array.
// This creates a 1 row x N column array. If the range to overwrite was not a
// single row, a different approach would be needed.
toEdit.setValues( [dataToWrite] );
}
}
Below is the most basic solution. At the end of this post, I'll expand on how this can be improved. I don't know how your data is organized, how exactly you generate new unique ids for your records, etc., but let's assume it looks something like this.
Suppose we need to update the existing record with new data. I assume your JSON contains key-value pairs for each field:
var chris = {
id:2,
name: "Chris",
age: 29,
city: "Amsterdam"
};
Updating a record breaks down into several steps:
1) Creating a row array from your object. Note that the setValues() method accepts a 2D array as an argument, while the appendRow() method of the Sheet class accepts a single-dimension array.
2) Finding the matching id in your table if it exists. The 'for' loop is not very well-suited for this idea unless you put 'break' after the matching id value is found. Otherwise, it will loop over the entire array of values, which is redundant. Similarly, there's no need to retrieve the entire data range as the only thing you need is the "id" column.
IMPORTANT: to get the row number, you must increment the array index of the matching value by 1 as array indices start from 0. Also, if your spreadsheet contains 1 or more header rows (mine does), you must also factor in the offset and increment the value by the number of headers.
3) Based on the matching row number, build the range object for that row and update values. If no matching row is found, call appendRow() method of the Sheet class.
function updateRecord(query) {
rowData = [];
var keys = Object.keys(query);
keys.forEach(function(key){
rowData.push(query[key]);
})
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets()[0];
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var idColumn = 1;
var ids = sheet.getRange(2, idColumn, sheet.getLastRow() - 1, 1).getValues();
var i = 0;
var matchedRow;
do {
if (ids[i] == query.id) { matchedRow = i + 2; }
i++;
} while (!matchedRow && i < ids.length);
if (matchedRow) {
var row = sheet.getRange(matchedRow, idColumn, 1, rowData.length);
row.setValues([rowData]);
} else {
sheet.appendRow(rowData);
}
}
NOTE: if your query contains only some fields that need to be updated (say, the 'id' and the 'name' field), the corresponding columns for these fields will be
headers.indexOf(query[key]) + 1;
Possible improvements
If the goal is to use the spreadsheet as a database and define all CRUD (Create, Read, Write, Delete) operations. While the exact steps are beyond the scope of the answer, here's the gist of it.
1) Deploy and publish the spreadsheet-bound script as a web app, with the access set to "anyone, even anonymous".
function doGet(e) {
handleResponse(e);
}
function doPost(e) {
handleRespone(e);
}
function handleResponse(e) {
if (e.contentLength == -1) {
//handle GET request
} else {
//handle POST request
}
}
2) Define the structure of your queries. For example, getting the list of values and finding a value by id can be done via GET requests and passing parameters in the url. Queries that add, remove, or modify data can be sent as payload via POST request. GAS doesn't support other methods besides GET and POST, but you can simulate this by including relevant methods in the body of your query and then selecting corresponding actions inside handleResponse() function.
3) Make requests to the spreadsheet URL via UrlFetchApp. More details on web apps https://developers.google.com/apps-script/guides/web
I need to add multiple values (ID fields of another custom list) to a Multiple Lookup field using Sp-services.
What is the correct format of the data to be used ?
I have tried like ( 5,9,6 ) but only selecting the first item.
I am not quite sure why do you want to do this, because these items you will add - they can't be saved as values, because there is a relationship between the column and lookup list, i.e. if you have Lookup column to the List1 and you add a new value from the List2 with id 99 and save, it will save the reference to the list item with id 99 in the List1.
but if anything, it is possible, this is how I am appending multiple lookup selected values:
var lines = additionalTechnologies.split(';#');
$.each(lines, function (index) {
if (lines[index].length < 3) {
$("select[title='Additional Technologies selected values']:first").append("<option value=" + lines[index] + " title=" + lines[index + 1] + ">" + lines[index + 1] + "</option>");
$("select[title='Additional Technologies possible values'] option[value=" + lines[index] + "]").remove();
}
});
and remove them from the all items list. just do it vice versa.
I have found a way to do it.
// "list1Id" contains the array of LIST1 ID fields that you want to add...
// "MULTIPLELOOKUPFIELD" is the multiple lookup field in the LIST2...
var multipleLookupValue ="";
for(i = 0; i < list1Id.length ; i++)
{
multipleLookupValue = multipleLookupValue + list1Id[i]+";#data;#";
}
var method = "UpdateListItems";
$().SPServices({
operation: method,
async: false,
batchCmd: "New",
listName: "LIST2" ,
valuepairs: [["MULTIPLELOOKUPFIELD",multipleLookupValue]],
completefunc: function (xData, Status) {
//alert("Added new item to LIST2 list");
}
});
May be it will help someone...
Friends'm working with NetSuite and SuiteScript. I can save a purchase order running the script and also charge Purchase Orders created, but when I bring returns data item value as a null value, and I need to get the id of the item.
The result gives me the log NetSuite is:
Purchase Order ID: 3706 Vendor ID: 144 Item ID: null Trandate: 06/08/2015 Form: Standard Purchase Order Currency: Peso CL
this happens all Purchase Orders and obviously if you have an item attached.
function to load javascript to use Purchase Order is as follows:
function loadPurchaseOrder(){
nlapiLogExecution('DEBUG','loadPurchaseOrder', 'Entra a funcion loadPurchaseOrder');
//se aplican filtros para la busqueda del objeto
var filters= new Array();
filters[0] = new nlobjSearchFilter('purchaseorder',null,'isnotempty');
filters[1] = new nlobjSearchFilter('mainline', null, 'is', 'T');
//seleccion de los campos que se quieren extraer
var columns = new Array();
columns[0] = new nlobjSearchColumn('item');
columns[1] = new nlobjSearchColumn('entity');
columns[2] = new nlobjSearchColumn('trandate');
columns[3] = new nlobjSearchColumn('customform');
columns[4] = new nlobjSearchColumn('currency');
columns[5] = new nlobjSearchColumn('internalid');
var results = nlapiSearchRecord('purchaseorder',null,filters,columns);
var out = "";
if(results != null ){
for(var i=0; i< results.length; i++){
var purchaseOrder = results[i];
var idItem = purchaseOrder.getValue('item');
var idVendor = purchaseOrder.getValue('entity');
var trandate = purchaseOrder.getValue('trandate');
var form = purchaseOrder.getText('customform');
var currency = purchaseOrder.getText('currency');
var idPurchaseOrder = purchaseOrder.getText('internalid');
out = " ID Purchase Order: " + idPurchaseOrder + " ID Vendor: " + idVendor + " ID Item: " + idItem
+ " Trandate: " + trandate + " Form: " + form + " Currency: " + currency;
nlapiLogExecution('DEBUG','purchaseOrderCargada', out);
}
}
return out;
}
If someone could please help me. Greetings!
pd:
I've also tried:
var idItem = nlapiGetLineItemField ('item', 'item');
and it does not work = /
This is maybe a longer answer than you're expecting, but here we go.
NetSuite divides Transaction records (Purchase Order is a type of Transaction) into Body and Line Item fields. When you do a Transaction search that includes mainline = 'T', you are telling NetSuite to only retrieve Body field data. The item field, however, is a Line Item field, so NetSuite will not return any data for it. That's why idItem is null.
Understanding the behaviour of the mainline filter is crucial to Transaction searches. Basically, it goes like this:
mainline = 'T' will only return body field data, so it will return exactly one search result per Transaction
mainline = 'F' will only return line item data, so it will return one search result for every line item on matching Transactions
mainline not specified will return both body field and line data, so it will return one result for each transaction itself plus one result for each line on each transaction.
Here's a concrete example. Let's say that there is only one Purchase Order in the system that matches all of your other search filters (besides mainline), and that Purchase Order has three items on it. This is how the search results will change based on the mainline filter:
If mainline = 'T' then you will get exactly one result for the Purchase Order, and you will only get data for Search Columns that are Body fields.
If mainline = 'F' then you will get exactly three results, one for each line item, and all of your Search Columns will contain data whether they are Body or Line fields
If mainline is not specified then you will get exactly four results, one of them will only contain data for Body fields, and the other three will contain both Line and Body data
It's difficult to advise on exactly how you should change your search as I do not know what you plan to do with these search results.
Having a problem with the code below only adds the latest value in the TestTaxonomyControl.Text to the metadata column. ( multiple values is turned on )
TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(String.Empty);
values.PopulateFromLabelGuidPairs(TestTaxonomyControl.Text);
TaxonomyField entKeyword = (TaxonomyField)item.Fields["Metadata"];
foreach (TaxonomyFieldValue value in values)
{
TaxonomyFieldValue term = new TaxonomyFieldValue("1;#" + value.Label + "|" + value.TermGuid);
entKeyword.SetFieldValue(item, term);
}
item.Update();
I'd say you only need something like:
TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(String.Empty);
values.PopulateFromLabelGuidPairs(TestTaxonomyControl.Text);
item["Metadata"] = values;
item.Update();