Встраивание javascript в Power Query через функцию Web.Page

Svetlana75

New Member
Joined
Jan 15, 2023
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Содержание задачи:

  • a = "100,500,0,0,2000,0,1000" ; - строка
  • b = 1000; - число
  • delim = " , " ;
  • Найти: result (т.е. в переменной result должна получиться после вычислений новая строка "0,0,0,0,1600,0,1000")
алгоритм решения простыми словами:

  1. переменную а преобразую в массив arr (переменная delim используется в преобразовании);
  2. создаю новый массив newarr, куда будут добавляться новые значения;
  3. от значения переменной b (1000) вычитаю первый элемент массива arr, получается 900 (положительная разность), поэтому значение первого элемента массива newarr равно 0, значение переменной b равно 900;
  4. от значения переменной b (900) вычитаю второй элемент массива arr, получается 400 (положительная разность), поэтому значение второго элемента массива newarr равно 0,значение переменной b равно 400;
  5. в результате вычислений с третьим и четвертым элементом массива arr получаем значения равные 0 (т.е. без изменений);
  6. пятый элемент массива newarr получает абсолютное значение 1600,значение переменной b равно о и вычисления прерываются;
  7. провожу конкатенацию массивов: newarr и arr (шестого и седьмого элемента)
Но я не могу увидеть результат в Power Query. При разворачивании таблицы отсутствует элемент BODY. В чем причина его отсутствия?
Безымянный.png

Power Query:
let   fx=(a as text, b, delim as text)=>
    
 Web.Page(
        "<script>
             var x = '"& a &"';
             var y = '"& Text.From(b) &"';
             var z = '"& delim &"'; 
             var arr = x.split(z);
             var arrnew = [];
             if (y > 0) {
                for (let i of arr) {
                    if ((y-i) < 0) {
                       i = -(y-i);
                       arrnew.push(i);
                       break;
                    }  
 
               y = y-i;
               i = 0;
               arrnew.push(i);
               }
            }
            arr.splice(0, arrnew.length);     
            var result = arrnew.concat(arr).join(z);
            document.write(result);       
          </script>")
in
fx
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Вопрос решен. Использовала цикл for(..;..;..), вместо for..of
Power Query:
let   
  fx=(a as text, b as number, delim as text)=>   
    Web.Page(
        "<script>
             x = '"& Text.From (a) &"';
             y = '"& Text.From (b) &"';
             z = '"& Text.From (delim) &"'; 
             arr = x.split(z);
             arrnew = [];
             if (y > 0) {
               for (i = 0; i < arr.length; i++) {
                    if ((y-arr[i]) < 0) {
                       arr[i] = -(y-arr[i]);
                       arrnew.push(arr[i]);
                       break;
                    }  

               y = y-arr[i];
               arr[i] = 0;
               arrnew.push(arr[i]);
               }
            }
            arr.splice(0, arrnew.length);     
            result = arrnew.concat(arr).join(z);
            document.write(result);       
         </script>")
in
fx
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top