VBA调用javascript(一)

函数接口

Function execJSFunc(filePath, funcName)
    Dim code
    Open filePath For Input As #1
    Do While Not EOF(1)
        Line Input #1, tmpCode
        code = code & tmpCode & Chr(13)
    Loop
    Close #1
 
    Set JS = CreateObject("ScriptControl")
    JS.Language = "JScript"
    JS.AddCode code
    Dim result
    result = JS.run(funcName, ThisWorkbook)
    execJSFunc = result
End Function

调用封装

Sub run(funcName)
    Dim path, fileName, pos, result
    path = ThisWorkbook.path
    pos = InStr(4, ThisWorkbook.Name, ".", 1)
    pos = Len(ThisWorkbook.Name) - 4
    fileName = Mid(ThisWorkbook.Name, 1, pos - 1)
    path = path + "" + fileName + ".js"
    result = execJSFunc(path, funcName)
    Debug.Print result
End Sub

调用示例

Sub 按钮1_Click()
    run("hello")
End Sub

test.js源码

function hello(workbook) {
    var sheets = workbook.sheets;
    sheets("Sheet1").range("a3").value = 55555;
    return workbook.sheets.count;
}

在x64 OFFICE中使用ScriptControl控件的方法

Sub Test()  
    Dim oSC As Object  
    Set oSC = CreateObjectx86("MSScriptControl.ScriptControl") ' create ActiveX via x86 mshta host  
    Debug.Print TypeName(oSC) ' ScriptControl  
    With oSC  
        '操作oSC  
    End With  
    CreateObjectx86 , True ' close mshta host window at the end  
End Sub
Function CreateObjectx86(Optional sProgID, Optional bClose = False)  
    Static oWnd As Object  
    Dim bRunning As Boolean  
    #If Win64 Then  
        bRunning = InStr(TypeName(oWnd), "HTMLWindow") > 0  
        If bClose Then  
            If bRunning Then oWnd.Close  
            Exit Function  
        End If  
        If Not bRunning Then  
            Set oWnd = CreateWindow()  
            oWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"  
        End If  
        Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID)  
    #Else  
        Set CreateObjectx86 = CreateObject("MSScriptControl.ScriptControl")  
    #End If  
End Function
Function CreateWindow()  
    Dim sSignature, oShellWnd, oProc  
    On Error Resume Next  
    sSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38)  
    CreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""about:<head><script>moveTo(-32000,-32000);document.title='x86Host'</script><hta:application showintaskbar=no /><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & sSignature & "',document.parentWindow);</script></head>""", 0, False  
    Do  
        For Each oShellWnd In CreateObject("Shell.Application").Windows  
            Set CreateWindow = oShellWnd.GetProperty(sSignature)  
            If Err.Number = 0 Then Exit Function  
            Err.Clear  
        Next  
    Loop  
End Function

pip 源切换至国内镜像

使用 pip 安装软件时,使用国内镜像可以大大提高下载速度

常用国内镜像
https://pypi.tuna.tsinghua.edu.cn/simple/ # 清华大学
https://mirrors.aliyun.com/pypi/simple/ # 阿里云
https://pypi.douban.com/simple/ # 豆瓣
https://pypi.mirrors.ustc.edu.cn/simple/ # 中国科学技术大学
https://pypi.hustunique.com/ # 华中科技大学
永久使用
在用户的根目录下创建 .pip 文件夹,新建 pip.conf 文件
示例:

cd ~
mkdir .pip
cd .pip
vim pip.conf

在文件中写入要使用的镜像
示例:
使用清华镜像

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

新方法

pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

Ubuntu下更新NVIDIA显卡驱动导致的重启无法进入操作系统

在外网上搜索一番后,终于找到了症结所在:Ubuntu内核bug导致gdm3和nvidia驱动冲突,使得gdm3无法正常启动图形界面,而gdm不断尝试启动的后果就是屏幕的闪烁。

解决方法

sudo apt purge gdm gdm3 # 卸载gdm和gdm3
sudo apt install gdm3 ubuntu-desktop    # 重新安装gdm3
systemctl restart gdm       # 重新启动gdm3服务

如果还是进不了。
看到登录界面,在右下角选Ubuntu wayload.

在Linux系统中使用Flatpak命令安装Scratch的方法

如果你的Linux操作系统支持Flatpak命令安装软件,那么可以使用Flatpak命令来安装Scratch,以下是具体方法。
使用Flatpak命令来安装Scratch

1、如果你的系统还没有安装Flatpack包管理器,请运行以下命令(以Ubuntu 20.04系统为例):

sudo apt-get update

sudo apt install flatpak
2、从Flatpak的Flathub仓库安装,运行以下命令:

flatpak remote-add –if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

flatpak install flathub edu.mit.Scratch

注:当前安装的版本为3.6.0。

3、安装完成后可运行以下命令运行:

flatpak run edu.mit.Scratch

注:可运行flatpak info edu.mit.Scratch命令查看相关的信息,返回如下信息即表示安装成功了:

Scratch – Create stories, games, and animations, share with others around the

world

ID: edu.mit.Scratch

Ref: app/edu.mit.Scratch/x86_64/stable

Arch: x86_64

Branch: stable

Version: 3.6.0

License: BSD-3-Clause

Origin: flathub

Collection: org.flathub.Stable

Installation: system

Installed: 513.0 MB

Runtime: org.freedesktop.Platform/x86_64/19.08

Sdk: org.freedesktop.Sdk/x86_64/19.08

Commit: be95cc888f17693303a1b56392c90b1915965c828b160023cb66c2bfffb75e53
Parent: 123e220a8e8392828b36807fec7ae55de9497d64054bf74e4847f464b4ce1123
Subject: Add –device=all for webcam access (64e7093d)
Date: 2020-06-15 05:11:25 +0000

Ubuntu中安装NVIDIA显卡驱动

a. ubuntu 18.04默认安装了第三方开源的驱动程序nouveau,安装nvidia显卡驱动首先需要禁用nouveau,不然会碰到冲突的问题,导致无法安装nvidia显卡驱动。

编辑文件blacklist.conf

sudo vim /etc/modprobe.d/blacklist.conf

安装或使用vi

在文件最后部分插入以下两行内容

blacklist nouveau
 
options nouveau modeset=0

更新系统

sudo update-initramfs -u

没有信息显示,说明nouveau已被禁用,接下来可以安装nvidia的显卡驱动。

2. 在英伟达的官网上查找你自己电脑的显卡型号然后下载相应的驱动。
3. 在ubuntu下按ctrl+alt+f6进入命令行界面,(进入命令行的方式可能有所不同,)

然后在命令行界面下输入:

sudo service lightdm stop      //这个是关闭图形界面,不执行会出错。(执行该语句时提示no lightdm service loaded ,跳过即可)

然后卸载掉原有驱动:

sudo apt-get remove nvidia-*  (若安装过其他版本或其他方式安装过驱动执行此项)

4.

给驱动run文件赋予执行权限:

sudo chmod  a+x NVIDIA-Linux-x86_64-版本号.run

安装:

sudo ./NVIDIA-Linux-x86_64-410.78.run -no-x-check -no-nouveau-check -no-opengl-files //只有禁用opengl这样安装才不会出现循环登陆的问题

no-x-check:安装驱动时关闭X服务

-no-nouveau-check:安装驱动时禁用nouveau

-no-opengl-files:只安装驱动文件,不安装OpenGL文件
出现找不到cc文件的问题,要执行下面的语句,否则安装完成,建立软连接也无法找到cc文件

sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install make

The distribution-provided pre-install script failed! Are you sure you want to continue? 选择 yes 继续。
Would you like to register the kernel module souces with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later? 选择 No 继续。
问题没记住,选项是:install without signing
问题大概是:Nvidia’s 32-bit compatibility libraries? 选择 No 继续。
Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up. 选择 Yes 继续

这些选项如果选择错误可能会导致安装失败,没关系,只要前面不出错,多尝试几次就好。

挂载Nvidia驱动:

modprobe nvidia

检查驱动是否安装成功:

nvidia-smi