Arrays 从数组中的正则表达式推送每个新匹配项
我尝试解析一些xml文件,代码如下:Arrays 从数组中的正则表达式推送每个新匹配项,arrays,regex,google-apps-script,Arrays,Regex,Google Apps Script,我尝试解析一些xml文件,代码如下: function parseXml() { var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml'; var xml = UrlFetchApp.fetch(url).getContentText(); var parseregexp = new RegExp (/.*em><no>(\d+)<\/no><
function parseXml() {
var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml';
var xml = UrlFetchApp.fetch(url).getContentText();
var parseregexp = new RegExp (/.*em><no>(\d+)<\/no><title>(.+?)<\/title><price vat="\w+">(\d+.\d+|\d+)<\/price><unit>(.+?)<\/unit><free>(\d+)<\/free>(.|\s)*?<it/g)
var parsedData = '$1 $2 $3 $4 $5 '
var rangeRegex = [];
var Pdata = xml.replace(parseregexp,parsedData)
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
ss.getRange(1, 1).setValue(Pdata)
}
在Pdata中的这个示例中,我从所有正则表达式匹配中得到了1个长字符串。如何从所有匹配项中生成包含5列的数组?我想把每一场比赛都推到阵列上,但不知道这是怎么回事。如果您有任何帮助,我们将不胜感激替代解决方案:
因为您试图访问xml数据,所以可以使用
然而,在fetch调用中似乎出现了问题,我无法获取整个数据。fetch提供了一个截断的文件,可能是超时的16mb文件,因此我下载了数据文件并将其上载到google drive。
此文件可用于解析XML数据,如下所示:
function parseXml() {
var file = DriveApp.getFileById("Xml File ID") //Get the id of the uploaded file and replace it for "Xml File ID"
var xml = file.getBlob().getDataAsString()
// The below code gave a error for XML parser
/*var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml';
var options = {
'method' : 'get',
'contentType': 'application/xml',
}
var xml = UrlFetchApp.fetch(url,options).getBlob().getDataAsString()
Logger.log(xml)*/
// End of code with gave an error
var arrayItems = []
var XmlElem = ["no","title","price","unit","free"] //Elements to look for
var document = XmlService.parse(xml);
var RCounter = 0
var groups = document.getRootElement().getChildren(); //GetGroup Element
for(var k = 0; k< groups.length; k++){ // Loop through each group element
var main = groups[k].getChildren() // Get sub groups in each group
for (var j=0 ; j < main.length; j++){ // Loop through each subGroups
var mainChilds = main[j].getChildren() //Get items in each subGroups
for (var l = 0 ; l < mainChilds.length; l++){ // Loop through each items
var items = mainChilds[l].getChildren(); // Get elemetns like "no","title","price","units","free" in each item
arrayItems[RCounter] = []
var total = 0;
for (var i = 0; i < items.length; i++) {
// Logger.log(items[i].getName())
var index = XmlElem.indexOf(items[i].getName()) //Look for items and place the value at corresponding index
if(index != -1)
arrayItems[RCounter][index] = items[i].getValue()
} //End Loope for elements
if(arrayItems[RCounter].length > 0) //in case the array is empty, reuse it
RCounter++
} // End loop for items
} // End loop for sub Groups
} // End loop for Groups
Logger.log(arrayItems)
}
希望有帮助 您应该发布分配给变量Pdata的值。否则我们必须运行您的代码。我们不应该那样做。看起来您可能需要将数据拆分为数组中的元素。@SandyGood是的,您是对的,我在问题中添加了它。您想将内容拆分到哪里?48514和48515?我想在单元格A2和A3中存储48514和48515。在B2 C2 D2 E2和B3 C3 D3 E3中,从$2$3$4$5 entires到$3$4$5 entires的下一场比赛,您已经厌倦了使用它!是的,它很好用,杰克!结合下载功能,它提供了刷新数据的绝佳工具。但我只有一个问题,如果我只是更改XmlElem var,那么这段代码是否适用于其他具有不同结构的xml文件?如果xml文件的结构保持不变,是的!上面的解决方案寻找以下结构{group}{group}{item}{XML元素}{/item}{/group}{/group}{/group}嘿,杰克!你们创作的剧本一直完美到今天。。我有一个错误:XML文档结构必须在同一个实体中开始和结束。at字符串:var document=XmlService.parsexml;我假设该文件可能会加载错误,将来如何避免?如果这对你来说很困难,那么如果我的评论没有得到回答也没关系
48514 The workpiece is the rod d 8x150mm P6AM5 HRC 64-66" CNIC" 154.58 Pcs 59 48515 The workpiece is the rod d 8x200mm P6AM5 HRC 64-66"CNIC" 198.24 pcs 32
function parseXml() {
var file = DriveApp.getFileById("Xml File ID") //Get the id of the uploaded file and replace it for "Xml File ID"
var xml = file.getBlob().getDataAsString()
// The below code gave a error for XML parser
/*var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml';
var options = {
'method' : 'get',
'contentType': 'application/xml',
}
var xml = UrlFetchApp.fetch(url,options).getBlob().getDataAsString()
Logger.log(xml)*/
// End of code with gave an error
var arrayItems = []
var XmlElem = ["no","title","price","unit","free"] //Elements to look for
var document = XmlService.parse(xml);
var RCounter = 0
var groups = document.getRootElement().getChildren(); //GetGroup Element
for(var k = 0; k< groups.length; k++){ // Loop through each group element
var main = groups[k].getChildren() // Get sub groups in each group
for (var j=0 ; j < main.length; j++){ // Loop through each subGroups
var mainChilds = main[j].getChildren() //Get items in each subGroups
for (var l = 0 ; l < mainChilds.length; l++){ // Loop through each items
var items = mainChilds[l].getChildren(); // Get elemetns like "no","title","price","units","free" in each item
arrayItems[RCounter] = []
var total = 0;
for (var i = 0; i < items.length; i++) {
// Logger.log(items[i].getName())
var index = XmlElem.indexOf(items[i].getName()) //Look for items and place the value at corresponding index
if(index != -1)
arrayItems[RCounter][index] = items[i].getValue()
} //End Loope for elements
if(arrayItems[RCounter].length > 0) //in case the array is empty, reuse it
RCounter++
} // End loop for items
} // End loop for sub Groups
} // End loop for Groups
Logger.log(arrayItems)
}