首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Excel VBA或Javascript替换编程代码中的时间值

使用Excel VBA或Javascript替换编程代码中的时间值
EN

Stack Overflow用户
提问于 2018-10-07 06:09:12
回答 1查看 92关注 0票数 0

我正在尝试制作一首歌曲的滚动卡拉OK歌词。歌词软件输出一个代码,其中包含我需要转换的以秒为单位的时间。

我有同一首歌的两个版本。一个运行在52 bpm (节拍每分钟),这是一个较慢的版本,另一个运行在104 bpm,这是一个更快的版本。

我已经完成了第一个版本,这花了我很多时间。我想以这样的方式转换代码,以便我可以自动获得更快的版本。

以下是示例代码

代码语言:javascript
复制
<item dStartTime="4" dEndTime="8" n3DRhythm="2" str3DSceneLayoutFile="">    
    <text>Batti Gul Meter Chalu</text>
</item> 
<item dStartTime="9.52" dEndTime="14.47" n3DRhythm="2" str3DSceneLayoutFile=""> 
    <text>rajj&lt;10.44&gt; ke&lt;10.99&gt; rulaya</text>
</item> 
<item dStartTime="14.47" dEndTime="19.06" n3DRhythm="2" str3DSceneLayoutFile="">    
    <text>rajj ke&lt;15.94&gt; hansaya</text>
</item> 

我想转换相同的代码,如下所示,以秒为单位的时间应该正好是两个小数点的一半

代码语言:javascript
复制
<item dStartTime="2" dEndTime="4" n3DRhythm="2" str3DSceneLayoutFile="">    
    <text>Batti Gul Meter Chalu</text>
</item> 
<item dStartTime="4.76" dEndTime="7.24" n3DRhythm="2" str3DSceneLayoutFile="">  
    <text>rajj&lt;5.22&gt; ke&lt;5.50&gt; rulaya</text>
</item> 
<item dStartTime="7.24" dEndTime="9.53" n3DRhythm="2" str3DSceneLayoutFile="">  
    <text>rajj ke&lt;7.97&gt; hansaya</text>
</item> 

我使用MS Excel中的代码,并尝试使用替换、拼接和其他一些公式来实现我所需要的,但我无法做到这一点,虽然excel vba可以帮助我,但我不能在vba中做到这一点。请求你们帮我解决这个问题,因为我需要转换很多歌曲。你的好心帮助会节省很多时间。任何帮助在excel vba,javascript或任何其他方式替换的代码。

<item>部分中,我需要将dStartTimedEndTime

<text>部分中,我需要转换任何看起来像数字的内容

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-07 06:21:02

在Javascript中,可以使用DOMParser将字符串转换为文档,之后可以遍历每个<item>,更改dStartTimedEndTime属性,然后更改子<text>节点中的任何数字:

代码语言:javascript
复制
const str = `<item dStartTime="4" dEndTime="8" n3DRhythm="2" str3DSceneLayoutFile="">    
    <text>Batti Gul Meter Chalu</text>
</item> 
<item dStartTime="9.52" dEndTime="14.47" n3DRhythm="2" str3DSceneLayoutFile=""> 
    <text>rajj&lt;10.44&gt; ke&lt;10.99&gt; rulaya</text>
</item> 
<item dStartTime="14.47" dEndTime="19.06" n3DRhythm="2" str3DSceneLayoutFile="">    
    <text>rajj ke&lt;15.94&gt; hansaya</text>
</item> `;

const doc = new DOMParser().parseFromString(str, 'text/html');
const halve = num => Math.round(100 * num / 2) / 100;
const halveAttrib = (node, name) => {
  const newVal = halve(node.getAttribute(name))
  node.setAttribute(name, newVal);
};

doc.querySelectorAll('item').forEach((item) => {
  halveAttrib(item, 'dStartTime');
  halveAttrib(item, 'dEndTime');
  item.children[0].textContent = item.children[0].textContent
    .replace(/\d+(\.\d+)/g, num => halve(num).toFixed(2));
});

const output = doc.body.innerHTML
  .replace(/dstarttime/g, 'dStartTime')
  .replace(/dendtime/g, 'dEndTime')
  .replace(/n3drhythm/g, 'n3DRhythm')
  .replace(/str3dscenelayoutfile/g, 'str3DSceneLayoutFile');
console.log(output);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52683756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档