Get hyper link from a SharePoint list column - javascript

I want to get the hyperlinks from a column of a SharePoint 2010 List. Right now the code gives me the hyperlink and the description concatenated together.
`
$(xData.responseXML).SPFilterNode("z:row").each(function () {
content = new Object(); //get new object
content.title = $(this).attr("ows_Title");
content.url= $(this).attr("ows_Url");`
The content.url gives me "http://www.example. ca,%20http://www.example. ca". I have tried split and then the URL doesn't work.
Any help will be greatly appreciated.
Thanks

if I'm understanding you correctly, split should work... here's an encapsulated example:
<a id="yourlinkId">link</a>
<script>
var url = "http://www.example.ca,%20http://www.example.ca";
var n = url.split(",%20");
var a = document.getElementById("yourlinkId");
a.href = n[0];
</script>
combine that with your code and you end up with something like:
$(xData.responseXML).SPFilterNode("z:row").each(function () {
content = new Object(); //get new object
content.title = $(this).attr("ows_Title");
var url = $(this).attr("ows_Url");
var n = url.split(",%20");
content.url = n[0];

Related

Getting out a product ID from URL - JavaScript

I'm trying to catch only a product ID from a URL. The ID is broke into 2 pieces:
https://www.example.org/category/first_part_of_ID-some_text-second_part_of_id
I was stuck with this function
var ft = "ft_";
var pod = "_";
var pageUrl = window.location.href;
var pageUrl1 = pageUrl.split("-")[1];
var pageUrl2 = pageUrl.replace("/","");
return pageUrl2;
}
Can anyone please help me clearing out everything except for the numbers? I tried with 2 different functions and putting it together, but it also didn't work.
This answer assumes the id you are trying to extract will always follow /category/ in the URL as well as that the first and second parts of the id always be located between the -some_text- part of the url
const url = window.location.href;
const urlAfterCategory = url.split('/')[4];
const id = `${urlAfterCategory.split('-')[0]}${urlAfterCategory.split('-')[2]}`
console.log(id); // Your id

Search a Spreadsheet for String and return partial row data

Hello stackoverflow community,
I want to create a script to search a google sheet by name, as this file will be deleted at the end of the script. A new file will be uploaded to google drive automatically every week. This will be the basis for the script that will be running weekly.
The code runs fine, but returns undefined in the 4 output cells.
Example Data from a row in the Spreadsheet "LocafoxInventoryData_Automatic":
1||5b8ff4fc3e578c005487dcd5|60.0000|38.6300|19.0000|||2.0000
I would like to retrieve the last number in the above string. (after the last " | " )
I hope someone can help me out. The strings being searched for are the ids of the products: In the above sample "5b8ff4fc3e578c005487dcd5". This id can only be found twice in the sheet. I want to always retrieve the first one. The differnce between the first and the second product IDs are the first number in the string. There are "1" and "2".
function getData() {
var files = DriveApp.getFilesByName('LocafoxInventoryData_Automatic');
while (files.hasNext()) {
var file = files.next();
var id = file.getId()
}
var sheetData = SpreadsheetApp.openById(id).getActiveSheet().getDataRange().getValues();
var sspre = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Vorlage");
var searchString1 = sspre.getRange('I7').getValue();
var searchString2 = sspre.getRange('I8').getValue();
var searchString3 = sspre.getRange('I9').getValue();
var searchString4 = sspre.getRange('I10').getValue();
for(var i=0;i<sheetData;i++)
{
//Column 1 should be searched
if(sheetData[i][0].search(searchString1)!=-1)
{
var rowData1 = sheetData[i];
return rowData1;
}
else if(sheetData[i][0].search(searchString2)!=-1)
{
var rowData2 = sheetData[i];
return rowData2;
}
else if(sheetData[i][0].search(searchString3)!=-1)
{
var rowData3 = sheetData[i];
return rowData3;
}
else if(sheetData[i][0].search(searchString4)!=-1)
{
var rowData4 = sheetData[i];
return rowData4;
}
}
sspre.getRange('D7').setValue(rowData1);
sspre.getRange('D8').setValue(rowData2);
sspre.getRange('D9').setValue(rowData3);
sspre.getRange('D10').setValue(rowData4);
file.setTrashed(true);
}
Returning Partial Row Data with Regex
Okay this is working for me. I have a file named 'LocafoxInventoryData_Automatic' which is a spreadsheet with one sheet. I look for files with that name and if more than one is found I throw error so that you can figure out which one you want.
I then open up the file as a SpreadSheet with openById. I get the product id from the Vorlage sheet column I7,8,9,10 and I wrap them with a regular expression that looks like this ^1\|\|cw8vtfxa5owglp03btv22g9d.\|\|\|(.)$ . Although in the code the backslashes \ have to be \\ double backslashed which took me a while to figure out.
So that get's the numbers at the end and I put them into Vorlage column D7,8,9,10. And then I set the file isTrashed to true. I don't know if you want to return the data so I left the object in there but commented them out. I also removed all of the else ifs.
function getData() {
var filename='LocafoxInventoryData_Automatic';
var files = DriveApp.getFilesByName(filename);
var n=0;
while (files.hasNext()) {
var file = files.next();
var id = file.getId();
n++;
}
if(n>1){throw(Utilities.formatString('Error: More than file named %s', filename));}//Will present an error if there is more than one file
if(id){
var sheetData = SpreadsheetApp.openById(id).getActiveSheet().getDataRange().getValues();//I assumed no headers in this file
var sspre = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Vorlage");
var re1 = new RegExp('^1\\|\\|' + sspre.getRange('I7').getValue() + '.*\\|\\|\\|(.*)$','i');
var re2 = new RegExp('^1\\|\\|' + sspre.getRange('I8').getValue() + '.*\\|\\|\\|(.*)$','i');
var re3 = new RegExp('^1\\|\\|' + sspre.getRange('I9').getValue() + '.*\\|\\|\\|(.*)$','i');
var re4 = new RegExp('^1\\|\\|' + sspre.getRange('I10').getValue() + '.*\\|\\|\\|(.*$)','i');
for(var i=0;i<sheetData.length;i++){
var rD1=re1.exec(sheetData[i][0]);
if(rD1) {
var rowData1 = rD1[1];
}
var rD2=re2.exec(sheetData[i][0]);
if(rD2) {
var rowData2 = rD2[1];
}
var rD3=re3.exec(sheetData[i][0]);
if(rD3) {
var rowData3 = rD3[1];
}
var rD4=re4.exec(sheetData[i][0]);
if(rD4) {
var rowData4 = rD4[1];
}
}
//var rObj={rowData1:rowData1,rowData2:rowData2,rowData3:rowData3,rowData4:rowData4};
sspre.getRange('D7').setValue(rowData1);
sspre.getRange('D8').setValue(rowData2);
sspre.getRange('D9').setValue(rowData3);
sspre.getRange('D10').setValue(rowData4);
file.setTrashed(true);
//return rObj;
}
}
Regular Expression exec method
RegExp
By the way it's quite helpful with sort of a problem to have your own regex tester written in the same language you happen to be developing in. I'm sorry it took me so long. If you run into additional problems let me know.

error : "Cannot convert Array to Object" Data Scraping script

I'm new to Javascript and even newer to google script, so please be comprehensive :)
I'm trying to build a little script to scrap some data from a bunch of URL. I' using Parser library. Here is what I've:
function getArray() {
var newData = new Array();
var sheet = SpreadsheetApp.openById('my_id').getSheetByName('Sheet4');
var urls = sheet.getRange(1,1,5,5).getValues();
var fromText = '<span class="nb-shares">';
var toText = '</span>';
for(i in urls){
var url = urls[i];
var content = UrlFetchApp.fetch(url).getContentText();
var scraped = Parser
.data(content)
.from(fromText)
.to(toText)
.build();
newData.push(scraped);}
var sheet2 = SpreadsheetApp.openById('my_id').getSheetByName('Sheet5');
sheet2.getRange(5, 1, newData.length, newData[1].length).setValues(newData);
}
It return me the following error : Cannot convert Array to Object
What I'm trying to do is looping on an URLs array so I can scrap some data from each one of these URL and return the results in my sheet.
Try changing newData.push(scraped) to newData.push([scraped])

parse all subpages of site

I tried to parse all sub pages of site with cheerio and request.
Before parsing I think that I need to collect all sub pages urls in an array.
But I don't understand why my code doesn't do it. Maybe anybody know in what my problem? Thanks for help.
Code that must collect subpages:
var validUrls = [];
var expression = /^(\d{1,3}-\d{1,3})/g;
var regex = new RegExp(expression);
var url = "http://www.fullautomatic.ru/index.php/";
if (url.match(regex)) {
validUrls.push(url.match(regex));
console.log(validUrls);
}
I think code example2 will be better for my task but i can't solve problem
var expression = /^(\d{1,3}-\d{1,3})/g;
var regex = new RegExp(expression);
var url = "http://www.fullautomatic.ru/index.php/";
for (var i in url + regex) {
console.log(regex[i]);
}

Unable to Split the array element in Javascript

I am quite new to Javascript. I wanted to get the filename and extension from a specific folder. For that i am using ActiveXObject and going to the folder using GetFolder and then enumerating through each individual files. The code is given below.
<html>
<script type='text/javascript'>
var myFileNameArray = new Array;
var myFileNameArray = new Array;
function ReadFromFile()
{
var i;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fsofolder = fso.GetFolder("C:\\Users\\Divya.R");
var colFiles = fsofolder.Files;
var fc = new Enumerator(colFiles);
for (; !fc.atEnd(); fc.moveNext() )
{
msg += fc.item() + ";";
}
myFilePathArray = msg.split(";");
for(i=0;i<=myFilePathArray.length;i++)
{
myFileNameArray[i] = myFilePathArray[i].split("\\");
}
document.write(myFileNameArray[0]);
}
</script>
<body onload='ReadFromFile()'>
</body>
</html>
I will get the complete file path in myFilePathArray from each array element i should get the filename. For that I am trying to split again based on '/' and then thought to get the arraylength-1 th element. However the last document write return be a blank page. It doesnt split the myFilePathArray. Please let me know what is wrong with this.
Regards,
Div
Since \ is an escape character it will be ignored. I have found a solution which worked for me. The code is given below.
var msg = "C:\\Users\\Divya.R\\Data.txt;C:\\Users\\Divya.R\\Test2.csv";
var regex = /\\/g;
var fileName="";
var FilePath = msg.replace(regex, "\\\\");
//document.write(FilePath);
var myArray1 = new Array;
var myArray2 = new Array;
myArray1 = FilePath.split(";");
myArray2 =myArray1[0].split("\\");
for(var i=0;i<=myArray1.length-1;i++)
{
myArray2= myArray1[i].split("\\");
fileName = fileName+myArray2[myArray2.length-1];
}
//alert(myArray2[myArray2.length-1]);
alert(fileName);
Regards,
Divya
A simple substr and lastIndexOf would suffice to get your parts:
var path = "C:\\Users\\Divya.R\\Data.txt";
var fileName = path.substr(path.lastIndexOf("\\") + 1);
var ext = path.substr(path.lastIndexOf('.') + 1); // txt

Categories

Resources